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.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- Keeping track of variable instances
Use reqclient: not designed for scripting purpose like
request
or many other libraries. Reqclient allows in the constructor specify many configurations useful when you need to reuse the same configuration again and again: base URL, headers, auth options, logging options, caching, etc. Also has useful features like query and URL parsing, automatic query encoding and JSON parsing, etc.The best way to use the library is create a module to export the object pointing to the API and the necessary configurations to connect with:
Module
client.js
:And in the controllers where you need to consume the API use like this:
reqclient
supports many features, but it has some that are not supported by other libraries: OAuth2 integration and logger integration with cURL syntax, and always returns native Promise objects.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... 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:
UPDATE
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:Microsoft typed-rest-client
This version is based on the initially proposed by bryanmac function which uses promises, better error handling, and is rewritten in ES6.
As a result you don't have to pass in a callback function, instead getJSON() returns a promise. In the following example the function is used inside of an ExpressJS route handler
On error it delegates the error to the server error handling middleware.
If you just need to make simple get requests and don't need support for any other HTTP methods take a look at: simple-get:
Unirest is the best library I've come across for making HTTP requests from Node. It's aiming at being a multiplatform framework, so learning how it works on Node will serve you well if you need to use an HTTP client on Ruby, PHP, Java, Python, Objective C, .Net or Windows 8 as well. As far as I can tell the unirest libraries are mostly backed by existing HTTP clients (e.g. on Java, the Apache HTTP client, on Node, Mikeal's Request libary) - Unirest just puts a nicer API on top.
Here are a couple of code examples for Node.js:
You can jump straight to the Node docs here