How to Build Microservices with Node.js

by Alexander Martin on Apr 25, 2023 Health & Fitness 468 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: 17020
Publishing
Articles: 79,390
Categories: 202
Online
Active Users: 1328
Members: 1
Guests: 1327
Bots: 16838
Visits last 24h (live): 11058
Visits last 24h (bots): 53649

Latest Comments

Basketball Games are more than just amusement; they assist players acquire useful skills. Playing fast-paced games enhances focus, hand-eye coordination, response time, and strategic thinking....
on Sep 10, 2026 about Top Amazon Automation Services
Relocating can be exciting, but the planning process can definitely become stressful when unexpected costs and mistakes appear. I agree that careful preparation makes a huge difference, from...
This is an interesting discussion of the reported effects and differences between Golden Teacher and Penis Envy mushrooms. I appreciate how the article highlights that experiences can vary and...
Building a custom home is an exciting experience, but managing the budget carefully is essential to avoid unexpected financial stress. From my experience, setting a realistic budget before...
The Human Benchmark Reaction Time Test is an online tool designed to measure how quickly a person responds to a visual stimulus.
This platform sounds really promising! I love the focus on safety and diversity – it's so important in online dating. Wishing everyone the best of luck finding their match, maybe even some fellow...
on Sep 8, 2026 about Nordic Online Dating
I saw a friend try a potent herbal supplement, convinced it was a harmless pick-me-up. The experience quickly devolved into a Buckshot Roulette of panic attacks and unsettling hallucinations....
Take your plans to another level with a company which takes pride in excellence and refinement. We have received numerous compliment regarding sophistication level associated with Russian...
I found your insights on unconditional love within the context of chat line dating quite illuminating! How do you think cultural nuances influence these signs? In a way, seeking meaningful...
girl games online players looking for creativity can try Toca Boca World, where every character, room, and story can become part of a unique adventure.
on Sep 7, 2026 about Nordic Online Dating

Translate To: