I need to add an additional attribute to the block after proxying the page before loading it with the browser. Server on Node.js In google I found this way of proxying a page on Node.js:

var http = require('http') var url = require('url') var server = http.createServer(function(request, response) { console.log(request.url) var ph = url.parse(request.url) var options = { port: ph.port, hostname: ph.hostname, method: request.method, path: ph.path, headers: request.headers } var proxyRequest = http.request(options) proxyRequest.on('response', function(proxyResponse) { proxyResponse.on('data', function(chunk) { response.write(chunk, 'binary') }) proxyResponse.on('end', function() { response.end() }) response.writeHead(proxyResponse.statusCode, proxyResponse.headers) }) request.on('data', function(chunk) { proxyRequest.write(chunk, 'binary') }) request.on('end', function() { proxyRequest.end() }) }).listen(8080) 

Is it possible, using this method at some stage, to obtain the html-code of the received page, make corrections to it and send the edited version to the browser for download?

  • Proxy is better to do on nginx. And there you can also change content on the fly - Total Pusher
  • @TotalPusher thanks for the tip. Dig in this direction. - Artemy Khodorev

1 answer 1

The essence of this script is to get the data in the buffer, make a replacement and give it back. The biggest problem for me was the encoding. Here is a working example:

 "use strict"; const http = require('http'), url = require('url'), // для установки этого модуля выполнить: npm i -S iconv Iconv = require('iconv').Iconv ; var server = http.createServer(function(request, response) { var ph = url.parse(request.url); var options = { port: ph.port ? parseInt(ph.port) : 80, hostname: ph.hostname, method: request.method, path: ph.path, headers: request.headers } var proxyRequest = http.request(options) proxyRequest.on('response', function(proxyResponse) { // буффер с телом ответа var buf = Buffer.alloc(0, 'binary'); proxyResponse.on('data', function(chunk) { // собираем тело ответа по частям (без заголовоков, только тело) buf = Buffer.concat([buf, chunk]); }); proxyResponse.on('end', function() { // по-умолчанию будем считать кодировку буфера текста latin-1, она же binary var headCharset = 'latin1'; // если в заголовках указана кодировка, используем ее var _m = proxyResponse.headers['content-type'].match(/charset=(.+)/i); if(_m) { headCharset = _m[1].toLowerCase(); } // превращаем бинарный буфер в строку utf-8 (внутренняя кодировка JavaScript) согласно определенной кодировки var iconv = new Iconv(headCharset, 'utf-8'); buf = iconv.convert(buf); var body = buf.toString('utf-8'); // Производим замену // Правильное определение кодировки очень важно, иначе символы, не входящие в latin-1, не будут заменены body = body.replace(/hello/gi, '-'); body = body.replace(/привет/gi, '-'); // Конвертируем назад в ту кодировку, которая была iconv = new Iconv('utf-8', headCharset); buf = Buffer.from(body, 'utf-8'); buf = iconv.convert(buf); // Меняем длину 'content-length', иначе клиент будет ругаться: // curl: (18) transfer closed with 12 bytes remaining to read proxyResponse.headers['content-length'] = buf.length; // отправляем заголовки response.writeHead(proxyResponse.statusCode, proxyResponse.headers); // отправляем тело response.write(buf, 'binary'); // конец передачи response.end(); }); }); request.on('data', function(chunk) { proxyRequest.write(chunk, 'binary') }); request.on('end', function() { proxyRequest.end() }); }).listen(8080); 

How to check out bash:

 export http_proxy=http://127.0.0.1:8080 curl http://site.ru/cp1251/ curl http://site.ru/utf-8/ 
  • Unable to install Iconv. "Error: Can't find Python executable" python ", you can set the PYTHON env variable." I understand correctly that you need to set the environment variable PYTHON? I did not hold a python in my hands and do not know this language. You can do without it? Or is it not about language? - Artemy Khodorev
  • npm init (if not already done) then npm i -S iconv , right? Python is here when node-gyp . What does which python say? - Total Pusher
  • Yes, npm inin did, the installation command is correct, yes. What is which python and where should it say what? I did not find such a line in the error log. - Artemy Khodorev
  • how is the system? This is the linux command for easy checking "where is python?". It must be entered into the console. I think you need to put a python. It is not needed by itself, but there is such a thing with npm packages - anything may be needed - Total Pusher