Microservices in NodeJS

Microservices architecture in NodeJS is a group of loosely-coupled services that works independently of each other. Failure in one service does not affect the other services in the system. Consider each service as a separate server or system.

For example, you have a social networking website. So you can create one microservice that handles all functions of users, one microservice for managing posts, one microservice for comments etc.

Today, we are going to create such microservices in Node JS. First, we are going to create 2 microservices. One for handling users and one for posts. Then we will create a gateway that will route the API requests to the relevant service.

  • Directory structure:
    • users
      • server.js
    • posts
      • server.js
    • gateway
      • server.js

Create users microservice

First, we are going to create a microservice for users. Create a folder named users and inside it create a file named server.js. After that, open command prompt or terminal in this users folder and run the following commands one-by-one:

> npm init
> npm install -g nodemon
> npm install express
> nodemon server.js

Following will be the code of your server.js file inside users folder.

const express = require("express")
const app = express()

app.get("/getData", function (request, result) {
	result.send("Get data from users.")
})

app.listen(3001, function () {
	console.log("User service is running.")
})

It simply creates a service running at port 3001. This will be your separate server for handling users requests.

Create posts microservice

Now we will create a microservice for handling posts requests. Create a folder named posts and inside it create a file named server.js. After that, open command prompt or terminal in this posts folder and run the following commands one-by-one:

> npm init
> npm install express
> nodemon server.js

The commands are same as we did in users folder. Open the server.js file inside posts folder and write the following code in it.

const express = require("express")
const app = express()

app.get("/getData", function (request, result) {
	result.send("Get data from posts.")
})

app.listen(3002, function () {
	console.log("Post service is running.")
})

It is almost similar to the user’s server.js file. Only the port number is different. It is running on port 3002.

So you have 2 different services that runs independently. If user server fails, the posts server will keep on running independently. Now we need to have a gateway in between them, that will route the users API requests to user’s server and posts API requests to post’s server.

Note that each miroservice should run on a different server or process. Because the whole point of creating microservices is to create loosely-coupled services.

Create a gateway

All the API requests will be sent to the gateway server. The gateway server will then redirect the request to the relevant service.

Create a folder named gateway. Create a file named server.js inside it and open command prompt in it. Then run the following commands in this folder one-by-one:

> npm init
> npm install fast-gateway
> nodemon server.js

We will be using fast-gateway NPM module. Following will be the code of server.js file inside gateway folder.

const gateway = require("fast-gateway")

const server = gateway({
	routes: [
		{
			prefix: "/users",
			target: "http://localhost:3001"
		},
		{
			prefix: "/posts",
			target: "http://localhost:3002"
		}
	]
})

server.get("/serverApi", function (request, result) {
	result.send("Server API is called.")
})

server.start(3000).then(function () {
	console.log("Server is started running.")
})

The gateway server will be running on port 3000. Run the following URL in your web browser:

http://localhost:3000/users/getData

You will see the message “Get data from users.” So the gateway will automatically route all the requests prefixed with “/users” to the user microservice. Same goes for posts. Run the following URL in web browser:

http://localhost:3000/posts/getData

Now you will see the message “Get data from posts.” That’s how you can create microservices in NodeJS.