In many node.js projects it is necessary to grab resources that may exist on some kind of external source. In general often you may just need to get what is there, just a simple get request, and thats it. It would also be nice to use some kind of package that helps to make it stupid easy, for this there is a popular npm package simply called request. request is one of many http clients that are available for a node.js environment, another popular such package would be axios. There is also not bothering with any npm package at all, and using a built in nopde.js module like that of http. However for the sake of this post I will be keeping the focus on request.
Using the npm package request is a simple as with any other npm package, just install with the install command giving the save flag if you want it added to a projects package.json file.
Say you just want to make a get request for something on the open Internet, like just the html of a page like www.google.com for example. In this section I will be covering thisn very basic use case example using request, and how it compares when using the http module.
Here is a quick demo as to how to go about making that simple get to google with request, by just giving the url, and a callback as the second argument.
The callback follows the usual deal that is expected with most callbacks in node.js where the first argument is an error if an error occurs. Then the second argument is a response object, and the third argument is the body of the request. For most requests there will be no need to parse the body manually, request will do that for you.
For the sake of comparison here is how you would go about making a method that will work in a similar fashion to this, just using the built in http module.
This method will still not work as well as what I can just quickly install using request, I could do a better job handling different protocols other than just http for example, notice that I do not give the http when giving a url, it will cause an error if I do. I could resolve that, but you get the idea, these things can eat up time. If I have all the time in the world maybe I would make my own http client, but why bother with that when there are so many other there all ready that work just fine to help put this to rest, and move on to more pressing matters.
When you compare the two it is easy to see why the request npm package helps to make grabbing at resources a little easer. As I see it, I do not think that it can be much more easy then the basic ger request example with request. At a minimum I would need to give a url, and a callback that has access to what is spit back at me when making the call, and thats it. I do not have to spend a good hour, or longer working out all the kinks that I run into when just using http.request in the node.js http module.
Head requests are a way to just grab some meta data information about a url, without downloading the actual content that may exist at the url. In some cases head requests com in handy as a way to reduce the amount of bandwidth use if I am working on some kind of project that involves a lot of requests, such as a web crawler. In this section I will be should some quick examples of using request to make head requests.
Say I just want to know the content-type header of a given url without downloading the whole page, I can just use request.head in the same way as I would request.get, or just the main request method that is exported when just giving a string as the first argument.
There is a body variable that is given as the third argument, but it will always be an empty string, as it should with head requests.
Like many of these use agents there is a method for each http request method that can be used, or I can just use the main function that is exported with an object that has a property for the http method that can be used to set the desired http method such as get,post,head, and so forth.
In this example I am doing just that calling the main method that is returned, and setting the http request method property to head, along with a custom user agent header, and a base url, allowing me to use the uri property as an end point from that base url.
So request is very much like many of these other popular http clients, that all seem to follow this convention to one extent or another. Allowing for easy basic use, but also allowing for more advanced use as well when called for.
So request has out of the box support for callback methods, and one other option which is streams. Streams are great when dealing with very large files, or any kind of live data stream. In this section I might not get into this in depth, but I have a post on using streams with request in which I do.
So for a fun example of using streams with request, I thought about making something that logs out the text of the book “war and peace” that is a book that is well know for it’s very high word count of over five hundred thousand words. In this example I will be using the transform method of the node.js built in stream module.
Streaming is useful if I am ever dealing with a very large file, such as this. instead of having to wait for the whole thing to download, I can start logging it to the console as it comes in.
So one nice think about the http client axios, is that it has promise support out of the box. Nothing against axios, that is a great client as well, but this post is on request. So out of the box there is no promise support. However there are additional packages that can be used to quickly add promise support using either native promises using request-promise-native, bluebird using request-promise, or another one using any-promise called request-promise-any
request is a descent http client, the out of box promises support might not be there, but with some projects I can do without it. Also When it comes to adding promise support I have more than one option to choose from which is nice. This rases all kinds of questions such as how does bluefish compare to just using native promises? So much to write about, and so little time. If you like this post be sure to check out my many other posts on node.js and npm packages.