Alex Craft
Alex Craft

Reputation: 15366

What's the difference between "http-proxy" and "request"?

I need to create forwarding proxy (not reverse proxy), there are two packages for Node.js http-proxy and request

I don't understand what's the difference between those in case of creating proxy? Are they doing exactly the same, or there are some tricky corner cases?

http-proxy

var http = require('http');
var proxy = require('http-proxy').createProxyServer();

http.createServer(function(req, res) {
  proxy.web(req, res, {
    target: "http://" + req.headers.host
  });
}).listen(3000, 'localhost');

request

var http = require('http');
var request = require('request');

http.createServer(function(req, res) {
  req.pipe(request(req.url)).pipe(res);
}).listen(3000, 'localhost');

Upvotes: 2

Views: 594

Answers (1)

Filip Dupanović
Filip Dupanović

Reputation: 33690

The two examples you've given are functionally the same, though I would still prefer http-proxy, as it already comes with some assumptions that you are specifically creating reverse/forward proxy requests.

Upvotes: 1

Related Questions