How to make REST API call from Express Application

Node.js

                At times our application might depend on other 3rd party REST APIs. In that case we might require to make either GET or POST requests from our express application.

               Here I am going to use node module called “request” HTTP client to make 3rd party API calls. There are so many other node modules which will provide functionality what we want. But, I have chosen “request” node module based on the usage stats. I always prefer to look at the usage stats of any node module before picking it to use. This shows the developer community adoption.

                 For the demonstration purpose  I am going to use express application which we created in the last article. In this example I am using “Elastic search” to get the product information. We need to make REST API call to the search server to get the product information. The GET request to the search server from the express is given below.

//Add required modules here
var express = require('express');
var request = require('request');
var app = express(); 
//http://localhost:3000/_getproduct/8821264 
app.get('/_getproduct/:id', function(req, res) { 
       if (!req.params.id) { 
           res.status(500); 
           res.send({"Error": "Looks like you are not senging the product id to get the product details."}); 
           console.log("Looks like you are not senging the product id to get the product detsails."); 
       } 
      request.get({ url: "http://localhost:9200/productcatalog/product/" + req.params.id },      function(error, response, body) { 
              if (!error && response.statusCode == 200) { 
                  res.json(body); 
                 } 
             }); 
     }); 

The explanation to the above code is given below.

request HTTP client GET call

In the coming article we will see how to connect to mongo db from Node.js. Till then, Stay Tune!!!

Advertisements

I am Siva Prasad Rao Janapati. Working as a software developer. Has hands on experience on ATG Commerce(DAS/DPS/DCS), Mozu commerce, Broadleaf Commerce, Java, JEE, Spring, Play, JPA, Hibernate, Velocity, JMS, Jboss, Weblogic,Tomcat, Jetty, Apache, Apache Solr, Spring Batch, JQuery, NodeJS, SOAP, REST, MySQL, Oracle, Mongo DB, Memcached, HazelCast, Git, SVN, CVS, Ant, Maven, Gradle, Amazon Web services, Rackspace, Quartz, JMeter, Junit, Open NLP, Facebook Graph,Twitter4J, YouTube Gdata, Bazzarvoice,Yotpo, 4-Tell, Alatest, Shopzilla, Linkshare. I have hands on experience on open sources and commercial technologies.

Tagged with:
Posted in Node.js

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

DZone

DZone MVB

Java Code Geeks
Java Code Geeks
%d bloggers like this: