Basic HTTP calls using Node.js

Node.js is designed for developing scalable network applications. We may face a situation where we need to perform HTTP calls from Node server to external server.

Request is one of the popular node package which is designed to simplify HTTP calls and it does. We are going to do use this node package for performing HTTP calls.

npm install --save request

In this tutorial i am going to discuss how to perform following HTTP calls using request. Click on the HTTP calls below to directly jump to particular paragraph.

GET Request:

GET is a http call to retrieve or fetch data from server by providing specific parameter in URL’s. GET is fast and used widely in web server to server static pages.

Code example :

var request=require("request");
request.get("http://codeforgeek.com",function(error,response,body){
           if(error){
                 console.log(error);
           }else{
                 console.log(response);
         }
});

Output :

s)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];
a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'
script','//www.google-analytics.com/analytics.js','ga');
ga('create','UA-46847736-1','auto');ga('send','pageview');
</script><div class="top-menu"> <span><a href="http://codeforgeek.com">
<i style="font-weight: 800;font-size: 27px;padding-left: 5px;font-style: normal;
color: #333;font-family: 'Audiowide', cursive;">CodeForGeek</i></a>
 <i class="fa fa-search" style="font-size:32px;margin-left: 53px;"></i></span>
<input type="search" id="keywords" size="50" placholder="Type in to Search."
style="width: 40%;height: 82%;border-bottom: 2px solid black
;background: transparent;margin-left: 4px;
.......continue

You can provide http authentication or additional URL variables too.

POST request :

POST is http call to send some data to server. POST request is very useful for performing some secure operations such as creating new resource at server, performing login etc.

Code example:

var request=require("request");
 var options = {
          method: 'POST',
          uri: '-----URL---------',
          form: {
            var_1:var_1,var_2:var_2,var_3:var_3
            },
          headers: {
            '----HEADER DATA------'
            }
        };
  request(options, function(error, response, body) {
               if(error){
                  console.log(error);
             }else{
                  console.log(response);
            }
        });
});

PUT request:

PUT is HTTP call to store the particular data to the web server. It takes endpoint URL as input and replies with response, body.

Code example:

var request=require("request");
request.put('http://mysite.com/img.png',function(error,response,body){
       if(error){
                  console.log(error);
             }else{
                  console.log(response);
                  console.log(response);
            }
});

DELETE request :

As name implies DELETE is http command which is used to delete some resource from the server. You have to provide endpoint url to execute this operation.

var request=require("request");
request.del("-------url------",function(error,response,body){
if(error){
                  console.log(error);
             }else{
                  console.log(response);
                  console.log(response);
            }
});

Further reading: