The proxy server is running locally. From the client it receives http.request , which it transmits to a server on the network. From the server on the network, the proxy server receives the response, replaces the HTTP headers and then sends them to the client.

I write a proxy on Node.js. I can not get the title by the client. Node.js is optional - in any way possible.

Code edited

 var http = require('http'); http.createServer(onRequest).listen(9000); function onRequest(req, res) { var options = { hostname: 'www.google.ru', port: 80, path: req.url }; var proxy = http.request(options, function(res) { res.on('end', function() { res.headers['Random-Header'] = 'Random-Content'; }); res.pipe(res, { end: true }); }); req.pipe(proxy, { end: true }); } 

Update

I tried to move the line http.createServer(onRequest).listen(9000); so that it was after the determination of onRequest , but nothing has changed.

  • Maybe the problem is that onRequest defined after the first use? - dzhioev
  • First, what about the fact that you get two variables res ? You are trying to set the value of the wrong variable. An example . Secondly, the documentation indicates response.setHeader(name, value) - perhaps the option via .headers[...] not correct. Thirdly, I don’t understand Node.js, I see. .pipe() , but I still have doubts: does the client respond after .pipe() response from a remote server? - Regent
  • function funcName(){} raised by the interpreter, the function declared in this way can be safely used in the code above its declaration. - NumminorihSF
  • @Aktash with response.setHeader you got excited: you have the variable res called. By the way, the header must be set before sending the data. And you _res.on('end' it is installed at the very end ( _res.on('end' ). - Regent
  • @Regent Two res corrected. .headers[...] replaced with response.setHeader . I write with Node.js for the first time, so I’m looking badly in the documentation and can’t find how .pipe() works in detail. I did not find yet a way to check whether the response is sent to the client only after the response from the remote server has arrived. If the proxy server is started with this code, then at http://127.0.0.1:9000/ Google opens in the browser, so I suspect that the routing still works correctly. - Aktash

1 answer 1

In the final version of the code (compared to the original), three things have changed:

  • Identical res variables in functions received different names ( res and _res ). The header must be set for the response sent to the client ( res ).
  • res.headers[...] been replaced by .res.setHeader(...) . Not the fact that it was necessary, but the setHeader method is specified in the documentation.
  • The string res.setHeader was extracted from the end event handler so that the header is set at the beginning of the response.

The code itself:

 var http = require('http'); http.createServer(onRequest).listen(9000); function onRequest(req, res) { var options = { hostname: 'www.google.ru', port: 80, path: req.url }; var proxy = http.request(options, function(_res) { res.setHeader('Random-Header', 'Random-Content'); _res.pipe(res, { end: true }); }); req.pipe(proxy, { end: true }); }