Photo by on Nikola Knezevic Unsplash On my post, I explained how a messaging system can help our system more scalable. previous Back on when we use microservices architecture, the main concept is separating each service. After separating the services, now we have multiple services that deployed on the different host. It means our client — frontend services like Web, Mobile, or CLI — should retrieve data from the different host. This situation will become harder for us when we had more services. We should remember each service’s host so we can retrieve the data. Here how the system will look like. The current system design So, we need a mechanism to enable our frontend service call the backend services without knowing where the backend service hosted. How we can create the mechanism? API Gateway — the gate you escape The solution that we can do is by using API Gateway. I don’t want to explain what is API Gateway here because there are a lot of great articles explained it, you can find , , and . here here here Based on , one of the objectives of API Gateway is to enable request rerouting. Request rerouting will enable our frontend service call the backend services and API Gateway will do it for us. Here will our system design will be when we implement API Gateway on our services. Nginx’s great article After API Gateway implemented Create one API Gateway for Me, please Actually, there are few API Gateway providers out there, like , , , and . But here I want to create my own because hands-on on something makes me more understand what kind of stuff it is. Nginx Plus Amazon API Gateway IBM API Connect Microsoft Azure API Management Now I want to create my own API Gateway using Express JS. Before it, I need to explain how the current system is. Currently, I had two services, Feed Service and Hashtag Service. The API Gateway will reroute the incoming request to which service should handle it. On my Feed Services, I had three endpoints to get all the feeds GET /feeds to get all feeds that contain the hashtag GET /feeds/{hashtag} to create a new feed POST /feeds On my Hashtag Services, I had two endpoints to get all the hashtags GET /hashtags to get single hashtag by its name GET /hashtags/{name} The target is when the frontend service sends the request to the API Gateway, the request will be rerouted to the which service should handle it. Look the picture below how the API Gateway reroute the request. API Gateway reroute upcoming request Create a new Express JS project To create a new Express JS project, you can follow the official website . here Create index.js Before creating index.js, install to enable us parsing the POST data. Run this command to install body-parser body-parser npm install -s body-parser The will become our entry point. Here our index.js index.js To run the server, run this command node index.js And then access and make sure it works. Now, we will create router as entry point to our API Gateway. First, create new directory at the root folder, name it . Second, create four files on the directory, those are , , , and . Here the purpose of each file localhost:3000 routers router router.js apiAdapter.js feedService.js hashtagService.js is to combine all the services endpoints router.js is to construct the API endpoint for each service apiAdapter.js is the file to reroute request to Feed Service feedService.js is the file to reroute request to Hashtag Service hashtagService.js Make your , , and like the codes below router.js feedService.js hashtagService.js After updating those files try to re-run the server and access each endpoint and make sure that everything is work well. But until this point we didn’t reroute any request. So how we do it? First, we need to install first. We use Axios to build the HTTP client. To install Axios, run this command Axios npm install -s axios After Axios installed, now write this code on your apiAdapter.js Then, update your and like code below feedService.js hashtagService.js The purpose of the code above is to creating the HTTP client for each service. We construct new Axios object for each service and pass the as parameter. The is the base URL for each services. So, when there is incoming request to the API Gateway, it will be handle by the Axios object that constructed using the before. Done! As simple as that. BASE_URL BASE_URL BASE_URL That is all how I create my own API Gateway using Express JS. Actually, API Gateway not only for rerouting request. API Gateway also used as the authenticator service, handle data caching, and as response aggregator. Thanks a lot to for taught me how to write a good code design. Oh ya, the API Gateway on my repository had been modified, JWT added to handle the authentication. You can find my full code here Jonathan Natanael Siahaan _Simple API Gateway using Express JS. Contribute to ecojuntak/api-gateway development by creating an account on GitHub._github.com ecojuntak/api-gateway _Simple REST API using Golang. Contribute to ecojuntak/hashtag-api development by creating an account on GitHub._github.com ecojuntak/hashtag-api _Simple REST API using Lumen. Contribute to ecojuntak/feed-api development by creating an account on GitHub._github.com ecojuntak/feed-api Cappy Hoding! 😎 💻 ☕️