How to make external HTTP requests with Node.js
Httpnode.jsHttp Problem Overview
The question is fairly simple. I want to use a Node.js server as a proxy to log, authenticate, and forward HTTP queries to a backend HTTP server (PUT, GET, and DELETE requests).
What library should I use for that purpose? I'm afraid I can't find one.
Http Solutions
Solution 1 - Http
NodeJS supports http.request as a standard module: http://nodejs.org/docs/v0.4.11/api/http.html#http.request
var http = require('http');
var options = {
host: 'example.com',
port: 80,
path: '/foo.html'
};
http.get(options, function(resp){
resp.on('data', function(chunk){
//do something with chunk
});
}).on("error", function(e){
console.log("Got error: " + e.message);
});
Solution 2 - Http
I would combine node-http-proxy and express.
node-http-proxy will support a proxy inside your node.js web server via RoutingProxy
(see the example called Proxy requests within another http server).
Inside your custom server logic you can do authentication using express. See the auth sample here for an example.
Combining those two examples should give you what you want.
Solution 3 - Http
You can use the built-in http
module to do an http.request()
.
However if you want to simplify the API you can use a module such as [superagent][1]
[1]: http://visionmedia.github.com/superagent/ "superagent"
Solution 4 - Http
node-http-proxy is a great solution as was suggested by @hross above. If you're not deadset on using node, we use NGINX to do the same thing. It works really well with node. We use it for example to process SSL requests before forwarding them to node. It can also handle cacheing and forwarding routes. Yay!
Solution 5 - Http
You can use node.js http module to do that. You could check the documentation at Node.js HTTP.
You would need to pass the query string as well to the other HTTP Server. You should have that in ServerRequest.url.
Once you have those info, you could pass in the backend HTTP Server and port in the options that you will provide in the http.request()