How to Build Microservices with Node.js

by Alexander Martin on Apr 25, 2023 Health & Fitness 482 Views

You must have a basic understanding of Javascript programming language if you wish to create microservices for real-world applications using NodeJS. In this article, we'll be building a microservice using a unique combination of external APIs and Node.js.

Building Microservices with Node.js

Prerequisites and initialization

First things first, to get the most out of this tutorial, you need to be familiar with the Javascript and HTTP requests. If you follow this process step by step, we will be able to create a real-world application of microservices using NodeJS and consuming the external API called ZipCodeAPI.com.

Now, before you get started, make sure you have installed the NodeJS development framework. After that, you have to prepare as following in your terminal:

  • First, create a directory named Microservices and then open that directory.
  • After that, you can utilize the Node package manager to acquire necessary dependency packages like Express and Request. (Express is one of the popular backend development frameworks of NodeJS and Request can help you communicate with external API).

You have to execute the order: 'npm i express request --save’ to install both dependency packages mentioned above.

Here 'i’ is the abbreviation for installation whereas the '--save’ is the attribute that allows the dependencies to be added to the package.json file. If you are using the latest version of the NPM packages then they will be added automatically but it's better to be safe than sorry. Therefore we have used the attribute '--save’.

In your NPM package, you will be provided with one more package namely node_modules which is a directory that can store the external modules that are required in your project.

Setting up the server

Now, you have to make a new entry file named server.js from the tree structure of this project. You will have to execute this file when we have to start our server. Add the code mentioned below in your server.js file:

const express = require('express');
const app = express();

app.use(express.json());

const routes = require('./api_routes/routes');
routes(app);
const port = process.env.PORT || 3000;

app.listen(port, () => {
    console.log(`Listening to port http://localhost:${port}`);
});

After passing the app instance to the routes object, we can create more routes in our primary file that we will need later in this project. After this, we will see how we can define these route files. When you are running the server, the app will be able to listen using port 3000 or any other port that has been specified in the code that is available in the PORT environment.

Specifying the routes

We have to create a file named quotes.js in the api_routed folder and add the code mentioned below:

'use strict';

const controller = require('../controllers/appController');

module.exports = (app) => {
    app.route('/about').get(controller.about);
    app.route('/distance/:zipcode1/:zipcode2').get(controller.getDistance);
}

/about and /distance/:zipcode1/:zipcode2 are two endpoints that are mentioned in the above code. The first one is used to provide details about the application. Meanwhile, the second one is used to calculate the distance in miles between two different zip codes with the help of an external API. The endpoints will be implied in the controller file, which we will discuss in the next step.

Building the controller

NodeJS uses the controller object to decode the user intent and then manage all the received requests using the route modules that we created earlier on in this guide. Now, you have to create a new file and name it controllers/appController.js. After that add the following code to the file:

'use strict';

const properties = require('../package.json');
const distance = require('../service/distance');

const controllers = {
    about: (req, res) => {
        const aboutInfo = {
            name: properties.name,
            version: properties.version,
        }
        res.json(aboutInfo);
    },

    getDistance: (req, res) => {
        distance.find(req, res, (err, dist) => {
            if (err)
                res.send(err);
            res.json(dist);
        });
    },
};

module.exports = controllers;

In this code, the controller object is seen to be carrying two functions, i.e. about() and getDistance().

The about function is used to accept two arguments, i.e. a pair of requests and responses. You can look up the package.json properties for the name and version values.

On the other hand, the getDistance function also possesses two arguments the same as about, a pair of requests and responses. It uses them to fund the defined API in the services folder.

Establishing the call

Now we have reached the stage where we have to make an API call. We will do that using the third-party API that we mentioned before in this article, i.e. ZipCodeAPI.com. You can get a free API key from them by simply registering for an account on their website. If you want, you can use the test API key in this example. But I have to warn you that it expires after a few hours.

To create the microservices, we already have stored API keys and the zipCodeURL in our environment variable. We have named them ZIPCODE_API_KEY and ZIPCODE_API_URL. So, now is the time to build our microservices using the code mentioned below:

const request = require('request');

const apiKey = process.env.ZIPCODE_API_KEY ||
    "hkCt1nW1wF1rppaEmoor7T9G4ta7R5wFSu8l1dokNz8y53gGZHDneWWVosbEYirC";

const zipCodeURL = process.env.ZIPCODE_API_URL;

var distance = {
    find: (req, res, next) => {
        request(zipCodeURL + apiKey +
            '/distance.json/' + req.params.zipcode1 + '/' +
            req.params.zipcode2 + '/mile',
            (error, response, body) => {
                if (!error && response.statusCode == 200) {
                    response = JSON.parse(body);
                    res.send(response);
                } else {
                    console.log(response.statusCode + response.body);
                    res.send({
                        distance: -1
                    });
                }
            });
    }
};

module.exports = distance;

Here we have to load the request package first. It will process the external HTTP request. After that, you can load your API key and zipCodeURL from the environmental variable.

In this code, we used the find function to build a request object and wrote a callback function there so that when the response is received, we will get a call. If the status of the response is HTTP 200 and there is no error in our function then it means that the body of the response is parsed and returned which indicates that it's successful. Now, if any errors have occurred then they will be logged directly into the console and the returned response will show -1.

Execution

Now, if you have arrived at this stage that means all went well with the development of microservices and now you can execute the code. You can do this by running NPM start in your project terminal. If it runs smoothly then you will be able to hit the about endpoint of the code and can see its output on your web browser.

nodejs-execute-1.jpg

Similarly, if you have passed some values in the query string and you have successfully hit the distance endpoint then you can see its output as well.

nodejs-execute-2.png

So, this was the process for building microservices using NodeJS to find out the distance between two different ZipCodes.

Challenges of Building Microservices in Node.js

One of the biggest challenges you face while building microservices is managing errors. If you want to build a successful microservice, then you must handle all errors gracefully, otherwise, your system will not be able to handle errors properly.

The second challenge is how to handle API calls. The API calls don’t have to be in a single location where they can be handled easily by the code base itself. You have to build an API gateway that will handle these API calls and return responses to the client applications for further processing.

Another challenge with building microservices is maintaining consistency across them. You don’t want your code base structure to change when adding new services to it because it will make it difficult for other developers who are working on different projects within your organization or company, so maintaining consistency across all of them is very important for your team members as well as for customers. For more about API, you may visit our API site for examples of how we provide web services.

The biggest challenge for building microservices in Node.js is the complexity of managing all the components involved. For example, you can't just write a C++ or Java application in your language of choice and expect it to seamlessly run on a server without any changes required.

Source: https://www.iplocation.net/how-to-build-microservices-with-node-js

Article source: https://article-realm.com/article/Health-Fitness/43314-How-to-Build-Microservices-with-Node-js.html

Comments

No comments have been left here yet. Be the first who will do it.
Safety

captchaPlease input letters you see on the image.
Click on image to redraw.

Reviews

Guest

Overall Rating:

Most Viewed Articles

Statistics

Members
Members: 17144
Publishing
Articles: 79,592
Categories: 202
Online
Active Users: 3173
Members: 2
Guests: 3171
Bots: 24753
Visits last 24h (live): 7457
Visits last 24h (bots): 49468

Latest Comments

" '훌륭한 유용한 리소스를 무료로 제공하는 가격을 알 수있는 웹 사이트를 보는 것이 좋습니다. 귀하의 게시물을 읽는 것이 정말 마음에 들었습니다. 감사합니다! 훌륭한 읽기, 긍정적 인 사이트,이 게시물에 대한 정보를 어디서 얻었습니까? 지금 귀하의 웹 사이트에서 몇 가지 기사를 읽었으며 귀하의 스타일이 정말 마음에 듭니다. 백만명에게 감사하고...
Discover the difference with our exclusive Escorts in Gurgaon that sets the standard for excellence. Our Call Girls are not just visually stunning but also skilled in the art of pleasure....
이러한 유익한 웹 사이트를 게시하는 데 아주 좋습니다. 웹 로그는 유용 할뿐만 아니라 창의적이기도합니다.  타잔토토  
모든 댓글을 읽는 데 시간이 걸렸지 만 기사를 정말 즐겼습니다. 그것은 나에게 매우 도움이되는 것으로 판명되었고 여기의 모든 댓글 작성자에게 확신합니다! 정보를받을 수있을뿐만 아니라 즐길 수있을 때 항상 좋습니다. 앙벳 주소  
Visiting the city becomes an extraordinary experience when accompanied by stunning Escort in Ghaziabad . Every meeting guarantees absolute privacy, gorgeous partners, and unforgettable moments of...
on Oct 3, 2026 about How to Start an Invention Idea
모든 댓글을 읽는 데 시간이 걸렸지 만 기사를 정말 즐겼습니다. 그것은 나에게 매우 도움이되는 것으로 판명되었고 여기의 모든 댓글 작성자에게 확신합니다! 정보를받을 수있을뿐만 아니라 즐길 수있을 때 항상 좋습니다. 타잔 도메인 주소  
나는 모든 것을 확실히 즐기고 있습니다. 훌륭한 웹 사이트이자 좋은 공유입니다. 감사합니다. 잘 했어! 여러분은 훌륭한 블로그를 만들고 훌륭한 콘텐츠를 가지고 있습니다. 좋은 일을 계속하십시오.   타잔카지노    
모든 댓글을 읽는 데 시간이 걸렸지 만 기사를 정말 즐겼습니다. 그것은 나에게 매우 도움이되는 것으로 판명되었고 여기의 모든 댓글 작성자에게 확신합니다! 정보를받을 수있을뿐만 아니라 즐길 수있을 때 항상 좋습니다.   아이스토토    
모든 댓글을 읽는 데 시간이 걸렸지 만 기사를 정말 즐겼습니다. 그것은 나에게 매우 도움이되는 것으로 판명되었고 여기의 모든 댓글 작성자에게 확신합니다! 정보를받을 수있을뿐만 아니라 즐길 수있을 때 항상 좋습니다.   아이스토토    
이봐, 내가 만난 멋진 게시물이 지난 일주일 동안 비슷한 종류의 게시물을 찾고 있었지만 거의 발견하지 못했습니다. 대단히 감사 드리며 더 많은 게시물을 찾을 것입니다.  타잔 도메인 주소  

Translate To: