Node Get Request

Posted on  by admin

Modified2 months ago. How can I make an HTTP request from within Node.js or Express.js? I need to connect to another service. I am hoping the call is asynchronous and that the callback contains the remote server's response.

1212 gold badges4747 silver badges8383 bronze badges. 8383 bronze badges. Here is a snippet of some code from a sample of mine.

It's asynchronous and returns a JSON object. It can do any form of GET request. Note that there are more optimal ways (just a sample) - for example, instead of concatenating the chunks you put into an array and join it etc..

Node.js – Get Requests

Hopefully, it gets you started in the right direction:.

It's called by creating an options object like:. And providing a callback function. For example, in a service, I require the REST module above and then do this:.

If you're looking for async/await (linear, no callback), promises, compile time support and intellisense, we created a lightweight HTTP and REST client that fits that bill:.

99 gold badges8686 silver badges9595 bronze badges. 9595 bronze badges. Try using the simple http.get(options, callback) function in node.js:.

There is also a general http.request(options, callback) function which allows you to specify the request method and other request details.

173173 bronze badges. 142k4141 gold badges258258 silver badges284284 bronze badges. 258258 silver badges284284 bronze badges.