How to make external HTTP requests with Node.js


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.

3/17/2013 2:13:52 PM

Accepted Answer

NodeJS supports http.request as a standard module:

var http = require('http');

var options = {
  host: '',
  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);
11/1/2011 7:12:28 PM

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow