Node-fetch is an implementation of the native Fetch API for Node.js. Making HTTP requests with Node.js: the node-fetch module
In the next examples we'll see how to use a bunch of Node.js modules which support Promises out of the box. So far we've seen how to make HTTP requests in the most basic way with callbacks.īut there is a better (sometimes) way to handle async code: using Promises alongside with async/await. Making HTTP requests with Node.js: I Promise I'll be async It could be promisified with util.promisify or even better you could use request-promise, a request version which returns promises (and has less dependencies). The request module does not support Promises. Now, I wouldn't consider this a real problem, but if your goal is to make just an HTTP GET request, sticking with http.get will be enough to get the job done. There’s a drawback though: request relies on 22 dependencies. As you can see in the example, it is much more concise than http.get. Working with the request module is pleasing. request.get expects an url as a first argument and a callback as a second argument. You should be able to see the same output as in the previous example. It supports both HTTP and HTTPS and follows redirects by default. Request is one of the most popular NPM module for making HTTP requests with Node.js. Note: the request module has been deprecated in February 2020. Making HTTP requests with Node.js: the request module In the end, working with http.get could be slightly more verbose compared to other libraries but that shouldn't be considered a drawback. And that's both good and "bad": good because you will be tempted to dig further into the Node.js internals to learn more and "bad" because you are forced to do a lot of manipulation if you want to extract the JSON response. The http.ClientRequest object emits events that you can listen to. In a real program you may want to pass the response to a callback. Now, in this example I'm just logging the response to the console. That means, in order to manipulate the body of the response you have to listen for events: notice res.on() in the example. The returned response is an http.ClientRequest object. Https.get expects an url as a first argument and a callback as a second argument. Title : 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit' ,īody : 'quia et suscipit\nsuscipit recusandae consequuntur expedita' }
To start, create an empty folder and initialize the project: In the next examples we'll be making HTTP requests with Node.js by calling a convenient "fake" API: the JSON Placeholder API. Node.js can be used to serve a vast range of purposes: you can build a command line tool, a proxy, a web server, and in its simplest form can be used just for querying a remote API and returning the output to the user. Almost everything today is available behind an API: weather forecasts, geolocation services and so on.
The answer is simple: as a JavaScript developer you will interact every day with remote APIs and webservers.
Making HTTP requests with Node.js: why?Īt this point you might be asking "Why would I ever do an HTTP request?". In the following post we'll use async/await, introduced in Node 7.6.0. Also, make sure to have one of the latest versions of Node.js. To follow along you should have a basic understanding of JavaScript and ES6.
It could be an API, a website, or something else: at some point you will need some code to get meaningful data from one of those remote sources. HTTP requests are a means for fetching data from a remote source.