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 the 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 the 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 a 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 MongoDB from Node.js. Till then, Stay Tune!!!

Advertisement

Siva Janapati is an Architect with experience in building Cloud Native Microservices architectures, Reactive Systems, Large scale distributed systems, and Serverless Systems. Siva has hands-on in architecture, design, and implementation of scalable systems using Cloud, Java, Go lang, Apache Kafka, Apache Solr, Spring, Spring Boot, Lightbend reactive tech stack, APIGEE edge & on-premise and other open-source, proprietary technologies. Expertise working with and building RESTful, GraphQL APIs. He has successfully delivered multiple applications in retail, telco, and financial services domains. He manages the GitHub(https://github.com/2013techsmarts) where he put the source code of his work related to his blog posts.

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Dzone.com
DZone

DZone MVB

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