 Photo by [Nikola Knezevic](https://unsplash.com/@nknezevic?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&utm_medium=referral) On my [previous](https://hackernoon.com/messaging-system-hands-on-7dda1afded37) post, I explained how a messaging system can help our system more scalable. 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 [here](https://microservices.io/patterns/apigateway.html), [here](https://docs.microsoft.com/en-us/azure/architecture/microservices/gateway), and [here](https://smartbear.com/learn/api-design/api-gateways-in-microservices/). Based on [Nginx’s great article](https://www.nginx.com/blog/building-microservices-using-an-api-gateway/), 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.  After API Gateway implemented ### Create one API Gateway for Me, please Actually, there are few API Gateway providers out there, like [Nginx Plus](https://www.nginx.com/solutions/api-gateway/), [Amazon API Gateway](https://aws.amazon.com/api-gateway/), [IBM API Connect](https://developer.ibm.com/apiconnect/), and [Microsoft Azure API Management](https://azure.microsoft.com/en-us/services/api-management/). But here I want to create my own because hands-on on something makes me more understand what kind of stuff it is. 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 * `GET /feeds` to get all the feeds * `GET /feeds/{hashtag}` to get all feeds that contain the hashtag * `POST /feeds` to create a new feed On my Hashtag Services, I had two endpoints * `GET /hashtags` to get all the hashtags * `GET /hashtags/{name}` to get single hashtag by its 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](https://expressjs.com/en/starter/installing.html). #### Create index.js Before creating index.js, install [body-parser](https://www.npmjs.com/package/body-parser) to enable us parsing the POST data. Run this command to install body-parser npm install -s body-parser The `index.js` will become our entry point. Here our `index.js` To run the server, run this command node index.js And then access `localhost:3000` 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 `routers` . Second, create four files on the `router`directory, those are `router.js` , `apiAdapter.js` , `feedService.js`, and `hashtagService.js` . Here the purpose of each file * `router.js` is to combine all the services endpoints * `apiAdapter.js` is to construct the API endpoint for each service * `feedService.js` is the file to reroute request to Feed Service * `hashtagService.js` is the file to reroute request to Hashtag Service Make your `router.js`, `feedService.js`, and `hashtagService.js` like the codes below 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 [Axios](https://github.com/axios/axios) first. We use Axios to build the HTTP client. To install Axios, run this command npm install -s axios After Axios installed, now write this code on your `apiAdapter.js` Then, update your`feedService.js` and `hashtagService.js` like code below 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`BASE_URL` as parameter. The `BASE_URL` 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 `BASE_URL` before. Done! As simple as that. 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 [Jonathan Natanael Siahaan](https://medium.com/@jonathannatanaelsiahaan) 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 [**ecojuntak/api-gateway** _Simple API Gateway using Express JS. Contribute to ecojuntak/api-gateway development by creating an account on GitHub._github.com](https://github.com/ecojuntak/api-gateway "https://github.com/ecojuntak/api-gateway")[](https://github.com/ecojuntak/api-gateway) [**ecojuntak/hashtag-api** _Simple REST API using Golang. Contribute to ecojuntak/hashtag-api development by creating an account on GitHub._github.com](https://github.com/ecojuntak/hashtag-api "https://github.com/ecojuntak/hashtag-api")[](https://github.com/ecojuntak/hashtag-api) [**ecojuntak/feed-api** _Simple REST API using Lumen. Contribute to ecojuntak/feed-api development by creating an account on GitHub._github.com](https://github.com/ecojuntak/feed-api "https://github.com/ecojuntak/feed-api")[](https://github.com/ecojuntak/feed-api) Cappy Hoding! 😎 💻 ☕️