Implementing Microservice Architecture In Node JS

Read Time:1 Minute, 24 Second

📍 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.

📍 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.

—> 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

Enter fullscreen mode Exit fullscreen mode
  • Payment server.js

Enter fullscreen mode Exit fullscreen mode
  • API-Gateway

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.


CyberSEO Pro - OpenAI GPT-3 autoblogging and content curation plugin for WordPress