DEV Community

Cover image for Implementing Microservice Architecture In Node JS
Lovepreet Singh
Lovepreet Singh

Posted on

Implementing Microservice Architecture In Node JS

πŸ“ Introduction

πŸ™‚ As we have discussed in our previous blog "Monolithic vs Microservices: A Practical Approach". But today we're going to implement Microservices Architecture in NodeJS.

πŸ‘‰ You can use any technology like Spring, Python, etc. But we are going to demonstrate using NodeJS.

NodeJS Meme

πŸ“ Directory Structure

πŸ™‚ You can find the GitHub Repo (Kindly run npm install in Order, Payment, and API-Gateway directories before running) Here. We have two services Order and Payment with API Gateway.

NodeJS_Microservices
|
---> Order
|
------> server.js (Running on PORT 8081)
|
---> Payment
|
------> server.js (Running on PORT 8082)
|
---> API-Gateway
|
------> server.js (Running on Port 9091)

πŸ”₯ The structure of our services looks like this:-

πŸ“ Implementation

Microservices Architecture

πŸ™‚ Whenever a client makes a request to the API-Gateway, We have defined some routes (Using prefixes) that redirect the request to the appropriate service (Depends which route is called). Payment and Order services are independent means If one fails other will not be affected.

πŸ”₯ Also we can add Auth or Middlewares so that no one can call the services directly or without Authentication. We have implemented a very basic architecture.

  • Order Server.js
const express = require("express");
const app = express();

const port = 8081;
app.get("/order-list", (req,res)=>{
    let response = {
        data: {
            item: [
                {
                    id: 1,
                    name: 'order-1'
                },
                {
                    id: 2,
                    name: 'order-2'
                }
            ]
        }
    };
    res.status(200).json(response);
});
app.get("/", (req,res)=>{
    res.send("Order called");
});

app.listen(port, ()=>{
    console.log("Listening at localhost "+ port);    
})
Enter fullscreen mode Exit fullscreen mode
  • Payment server.js
const express = require("express");
const app = express();

const port = 8082;
app.get("/payment-list", (req,res)=>{
    let response = {
        data: {
            item: [
                {
                    id: 1,
                    name: 'Payment-1'
                },
                {
                    id: 2,
                    name: 'Payment-2'
                }
            ]
        }
    };
    res.status(200).json(response);
});

app.get("/", (req,res)=>{
    res.send("Payment called");
});

app.listen(port, ()=>{
    console.log("Listening at localhost "+ port);    
})
Enter fullscreen mode Exit fullscreen mode
  • API-Gateway
const gateway = require("fast-gateway");

const port = 9001;
const server = gateway({
    routes: [
        {
            prefix: "/order",
            target: "http://localhost:8081/",
            hooks: {}
        },
        {
            prefix: "/payment",
            target: "http://localhost:8082/",
            hooks: {}
        }
    ]
});

server.get('/mytesting', (req,res)=> {
    res.send("Gateway Called");
})

server.start(port).then(server=>{
    console.log("Gateway is running "+port);
})

Enter fullscreen mode Exit fullscreen mode

😌 Now, we can start the services including the gateway

Terminal running microservices

πŸ“ And we can request http://localhost:9001/order Or http://localhost:9001/payment


πŸ™‹ Follow for more in-depth tutorials. Right now, I am targeting beginners but soon more advanced things we'll discuss.

Drop your views in the Comment Box. Hope It helps.

Latest comments (59)

Collapse
 
dog1203 profile image
dog1203
Collapse
 
latoadeoye profile image
latoadeoye

Highly appreciated. Your posit is motivational. Thanks

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Thanks man ☺😊

Collapse
 
restdbjones profile image
Jbee - codehooks.io

Very good intro.
Any thoughts on covering also a serverless node.js approach?

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Will do that in the next one... Thanks

Collapse
 
snowowl profile image
Snow Owl

This is very crisp and well done.

If you don't want to spend a lot of time doing config, we at Snow Owl have enabled request-level routing, monitoring, and transformations for microservices without using Kubernetes. This is done by creating a reverse proxy + API gateway + rules engine.

It's also SaaS, no-code, serverless, and sits on the edge. , we set up in 15 minutes and scale easily. DM me if you want to beta test! Snowowl.co

Collapse
 
victorioberra profile image
Victorio Berra

When will your site have docs, screenshots and info?

Collapse
 
snowowl profile image
Snow Owl

You can check out docs.snowowl.co for docs, screenshots, and video. Also if you drop your email in the homepage I'll send you an email with beta login!

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Ok.

Collapse
 
gulshanaggarwal profile image
Gulshan Aggarwal

Nice!

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Thanks Gulshan

Collapse
 
pratikshirsath54 profile image
Pratik Shirsath

Easy to Understand. Looking forward to implement this. Can I have more details on this.

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Thanks. Will do soon πŸ˜ƒ

Collapse
 
tahadostifam profile image
Taha. Dostifam

Would you suggest a directory structure for Microservice in Nodejs?

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

It is written in the blog

Collapse
 
arjunbv777 profile image
Arjun

Better use nest js framework.. gives lot options for communicating with other services and for rest use express as it is in the nest.

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Will look into it. Thanks for pointing out buddy

Collapse
 
rescenic profile image
Muhammad Ridwan Hakim

Thank you. Please continue for in-depth tutorials.

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Sure. No problem. Kindly share the content with your friendsπŸ‘­πŸ‘¬

Collapse
 
nadavl profile image
Nadav Lebovitch

Nice, very neat.
Usually in a monorepo it's helpful to have a packages manager such as Turborepo or NX to install all the packages and dependencies.

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Noted Nadav. Thanks for your view

Collapse
 
restdbjones profile image
Jbee - codehooks.io

Cool stuff!

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Thanks bruh

Collapse
 
frankrsc profile image
Francisco Flores

can you do something like that but using docker?

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Sure, we can do that. Wait for more blogs in future, will do that

Collapse
 
frankrsc profile image
Francisco Flores

Nice! thanks, nice blog

Thread Thread
 
lovepreetsingh profile image
Lovepreet Singh

Thanks ☺

Collapse
 
muzzammil194 profile image
Muzzammil Shaikh

Nice content

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Thanks brother

Collapse
 
arielro85 profile image
arielro85

Easy to Understand. Thanks for this

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Thanks πŸ™‚

Collapse
 
murilofranceschetto profile image
Murilo Gheller Franceschetto • Edited

Can anyone tell if this way is better than using nginx proxy with upstream and proxy_pass, if so why?

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

You can go with NGINX too, We did covered it in our previous blog

Some comments may only be visible to logged-in visitors. Sign in to view all comments.