How to make external HTTP requests using Node.js

The question is pretty simple. I want to use the Node.js server as a proxy to register, authenticate and redirect HTTP requests to the server HTTP server (PUT, GET and DELETE requests).

What library should I use for this purpose? I'm afraid I can't find him.

+80
Nov 01
source share
6 answers

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); }); 
+130
Nov 01 '11 at 19:12
source

I would combine node-http-proxy and express .

node-http-proxy will support a proxy server inside your node.js web server via RoutingProxy (see the example with proxy requests in another http server).

Inside user server logic, you can authenticate using express. See auth sample here for an example .

Combining these two examples should give you what you want.

+7
Nov 01 '11 at 13:45
source

I recently discovered REQUESTIFY .

Receive request:

 requestify.get('http://example.com').then(function(response) { // Get the response body response.getBody(); }); 

Post in Json:

 requestify.post('http://example.com', { hello: 'world' }) .then(function(response) { // Get the response body (JSON parsed or jQuery object for XMLs) response.getBody(); // Get the raw response body response.body; }); 

Hope this helps

+7
Mar 30 '16 at 15:45
source

You can use the built-in http module to do http.request() .

However, if you want to simplify the API, you can use a module like superagent

+4
Nov 01
source

node -http-proxy is a great solution as suggested by @hross above. If you are not out of date when using node, we use NGINX to do the same. It works great with node. We use it, for example, to process SSL requests before sending them to node. It can also handle caching and forwarding routes. Hurrah!

+1
Nov 01 '11 at 17:19
source

You can use node.js http module for this. You can check the documentation on Node.js HTTP .

You will also need to pass the query string to another HTTP server. You must have this in ServerRequest.url .

Once you have this information, you can pass the HTTP server and port as options that you provide in http.request()

0
Nov 01 '11 at 13:23
source



All Articles