Node.js in Action: Understand Request Module

This is the node.js in action article series. In this series we are learning node.js with practical examples. We have covered many things that you can learn by clicking the following links.

This article explains the “request” module in node.js. First of all the “request” module is not a core module of node.js, so we need to install it on our own. I hope the node.js server is set up in your system and running. Then fine, to install the “request” module we need to fire the following command.

npm install request

This command will install the request module in the current node.js application. Here is the screen to demonstrate the installation.

install request module

Once we start the installation, we will find that it's downloading many files and installing in the current node.js application. You will see the following screen after finishing the installation.

finishing the installation

Now, we are sure that the “request” module is installed in the application and it's ready to use. Now, let's discuss it. What is the purose of the request module?

The beauty of node.js is, not only can we build a server but we can also build a client too using node.js. A client in the sense that we can call some other server from a node.js application and get data from there. And to get data from another server, we can use the “request” module in node.js.

Here is a simple implementation of the “request” module. In this module we are making a HTTP request to the Google server and then we are logging the response in the console. Have a look at the following example.

var http = require('http');
var
request = require("request");
var
server = http.createServer(function (req, res) {
request("http://www.google.com", function (error, response, body) {
     console.log(body);
  });
});
server.listen(9090);
 

Let's discuss the code a little bit. At the second line, we have initialized the “request” module to request a variable. Then within the body of the server , we are making a request call to the Google server. The request method takes two parameters, the first parameter is the domain name and the second parameter is the callback function. Once the request process finishes the callback function will execute.

Once we run the application, we will see that the entire response has been logged in the console window.

console window

The beauty of the request module is that, it will allow making many types of requests to the server, like GET, POST, PUT and DELETE. In this example we have created a GET request to get the web page content. If needed we can customize the request function like the following.

var request = require("request");
    request({
        uri: "http://www.someurl.com",
        method: "GET",
        timeout: 10000,
        followRedirect: true,
        maxRedirects: 10

    }, function (error, response, body) {
        console.log(body);

});
 

Conclusion

In this article we have learned about the “request” module with an example. I hope you have understood the uses of the “request” module. A future article will explain a few more important concepts of node.js.


Similar Articles