![]() ![]() #Nodejs http request installIf I were to make a super simple GET request I wouldn't install neither axios nor node-fetch. ![]() My rule of thumb is to pick the smallest library with the fewer dependencies possible based on what I want to do. ![]() Making HTTP requests with Node.js: conclusionsĪs with almost everything with JavaScript, sometimes picking one module over another it's a matter of personal preferences. There are also other libraries for HTTP requests in Node.js like bent, a modern alternative to request. Last but not least it has only 2 dependencies. It's super simple to use, highly configurable and intuitive. I really can't find any drawback in axios. #Nodejs http request codeIt's basically the same as window.fetch so if you're accustomed to use the original it won't be difficult to pick the Node.js implementation.Ĭonst axios = require ( "axios" ) const url = "" const getData = async url => getData (url ) Īgain, by running the above code you should be able to see the same output of the previous examples. Node-fetch is an implementation of the native Fetch API for Node.js. Making HTTP requests with Node.js: the node-fetch module #Nodejs http request how toIn 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.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |