Is there a solution that allows Node.js to act as a reverse HTTP proxy?

Our company has a project that right now uses nginx as a reverse proxy for serving static content and supporting comet connections. We use long polling requests to get rid of constant update requests and allow users to receive updates immediately.

Now I know that there is a lot of code for Node.js, but is there a solution that allows Node.js to act as a reverse proxy for serving static content, like nginx does? Or maybe there is a structure that allows you to quickly develop such a layer using Node.js?

+3
source share
2 answers

node-http-proxy sounds the way you want

var sys = require('sys'),
  http = require('http'),
  httpProxy = require('http-proxy').httpProxy;

http.createServer(function (req, res){
    var proxy = new httpProxy;
    proxy.init(req, res);
    proxy.proxyRequest('localhost', '9000', req, res);
}).listen(8000);

http.createServer(function (req, res){
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
    res.end();
}).listen(9000);</code></pre>
+2
source

dogproxy can help you if not as a complete solution, but perhaps as a building block for one.

However, you may need to reconsider saving nginx to serve static content - it is specifically designed and configured for this specific task. You would add a lot of overhead when using node.js to serve static content - just like using PHP to serve static files.

+3
source

Source: https://habr.com/ru/post/1731691/


All Articles