Build API Gateway with Nodejs for Microservice

illustration for api gateway

This article is about how to build a API Gateway for our microservice. Previously we already build a Property Service and User Service, so you can follow from beginning.

An API gateway takes all API calls from clients, then routes them to the appropriate microservice with request routing, composition, and protocol translation. Typically it handles a request by invoking multiple microservices and aggregating the results, to determine the best path. It can translate between web protocols and web‑unfriendly protocols that are used internally.
In this article, our API gateway act is be a gateway from client to access our services. And also next article we will learn how to set the authentication in our API gateway and set the middleware as well.

Next, we will start to build an API Gateway with Nodejs with this following steps:
- Create Express project
- Setup .env
- Create apiAdapter
- Create service handler
- Register to routes
- Run in Postman

Create Express Project

For create a project, you need install Nodejs and Express in your PC. Then, you can use this command

Setup .env

Create a .env and then we can set url services. Don’t forget to add dotenv to make our project can read the process env.

.env file

Create apiAdapter

Usually i create apiAdapter inside the routes folder, because we also will setup handler for other services.

inside routes folder

The apiAdapter is an utils for our gateway can access with services. For Http Request we will using axios. We create axios instance with timeout, so we can use this instace inside another files.

apiAdapter.js

Create Service Handler

To create a maintainable controllers, i create a folder for one service. So when we have new API for that service, it will easy to update the gateway.

service handler folder

now, we will create an API for get all house which from Property Service. First we need to import apiAdapter and property service url. Next we pass request to the Property Service by our apiAdapter. One important thing, we must set for fail scenario with set the catch with some condition such as service unavailable and error response.

getAll.js

Every service will have an index.js as parent file, this file just contain services we have.

index.js

Register to Routes

To make the controller able to use, we need to register into the routes file. Routes file also i split by its service. Here is the house routes

house.js

Last, we can register the spesific routes into app.js like this

app.js

Run in Postman

To make sure our API was running correctly, we can test it in Postman

run in postman

From the steps above we already build API Gateway with Express Nodejs. You can clone my project from my Github as well. Next, we continue to setup JWT in this Gateway API. See you!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store