Integrating Express API Gateway with Node.js: The Modern Microservices Approach

Ali Ahmad Jan
4 min readOct 20, 2023

--

Microservices have rapidly become a popular architectural style, enabling organizations to develop modular and scalable applications. Each microservice takes care of one specific functionality and can be developed, deployed, and scaled independently. With all these moving parts, there arises a need to efficiently manage and route incoming requests to appropriate services. Enter the API Gateway.

Advantages of an API Gateway:

  1. Centralized Management: Allows central management of all services without needing to touch each service individually.
  2. Security: Implement authentication and authorization at a single point instead of every microservice.
  3. Rate Limiting: Control the number of requests a user or service can make within a defined time limit.
  4. Caching: Improve response times and reduce unnecessary processing by caching responses.
  5. Load Balancing: Distribute incoming requests across multiple service instances to ensure optimal resource utilization.
  6. Monitoring: Centralized logging and monitoring capabilities to keep an eye on all incoming and outgoing requests.

Why Express API Gateway?

There are numerous API gateway solutions available, but Express API Gateway stands out for a few key reasons:

  • Simplicity: Built on top of Express.js, one of the most popular and minimal web application frameworks for Node.js.
  • Flexibility: Being open-source, it’s customizable to fit unique requirements.
  • Configuration over Code: YML or JSON configurations allow quick setup without delving into the code.
  • Built-in Policies: It comes with a plethora of built-in policies like logging, caching, rate-limiting, etc.
  • Developer Friendly: It can simplify the development process by handling cross-cutting concerns like authentication and authorization, allowing individual microservices to focus on their specific functionality.
  • Compatibility: It is easily compatible with Node.js making it easy to work with Node Applications

Setting Up Express API Gateway:

Firstly, install the Express Gateway:

$ npm install -g express-gateway

Initialize a new gateway:

$ eg gateway create
 ? What is the name of your Express Gateway? my-gateway
? Where would you like to install your Express Gateway? my-gateway
? What type of Express Gateway do you want to create? (Use arrow keys)
❯ Getting Started with Express Gateway
Basic (default pipeline with proxy)

Follow the prompts, and you’ll have a basic gateway set up in no time!

Configuration with YML:

The real power of Express API Gateway lies in its configuration. With a simple YML file, you can define all your routes, endpoints, and policies.

Here’s a basic gateway.config.yml configuration:

http:
port: 5000
admin:
port: 9876
host: localhost
apiEndpoints:
api:
host: 'localhost'
paths: '/endpoint/*'
serviceEndpoints:
service:
url: 'http://localhost:3000'
policies:
- proxy
pipelines:
default:
apiEndpoints:
- api
policies:
- proxy:
- action:
serviceEndpoint: service
changeOrigin: true

In this configuration:

  • Requests coming to http://localhost:5000/endpoint/* are routed to http://localhost:3000/*.
  • We’re using the proxy policy, which will pass along the request to our defined service endpoint.

Integration with Node.js:

Now, how does this work with a Node.js application?

Imagine you have a simple Express.js app running on localhost:3000.

const express = require('express');
const app = express();
app.get('/endpoint/hello', (req, res) => {
res.json({ message: "Hello from the microservice!" });
});
app.listen(3000, () => {
console.log('Service running on port 3000');
});

With the gateway in place and the above configuration, any request to http://localhost:5000/endpoint/hello will be seamlessly routed to our service, providing a centralized entry point without the client ever knowing about the underlying service or its actual location.

Interacting With Multiple Node Js Applications

Similarly with Express API Gateway, you can interact with multiple Node.js applications, each application running on different Port. And for this make work we have to change gateway.config.yml to handle both applications simultaneously

Here is an Example of how to do it:

http:
port: 5000
admin:
port: 9876
host: localhost

apiEndpoints:
user-api:
host: 'localhost'
paths: '/user/*'
camunda-api:
host: 'localhost'
paths: '/engine-rest/*'

serviceEndpoints:
camunda-service:
url: 'http://localhost:3000'
user-service:
url: 'http://localhost:3001'

policies:
- basic-auth
- cors
- expression
- key-auth
- log
- oauth2
- proxy
- rate-limit

pipelines:
user-pipeline:
apiEndpoints:
- camunda-api
policies:
- proxy:
- action:
serviceEndpoint: camunda-service
changeOrigin: true
task-pipeline:
apiEndpoints:
- user-api
policies:
- proxy:
- action:
serviceEndpoint: user-service
changeOrigin: true

To run API Gateway you have to type this command in terminal or run the URL in Postman:

curl http://localhost:5000/user/create

I would prefer to run the URL in Postman for better experience

Express API Gateway is easy to use, and as i said earlier its developer friendly so working with it makes things more simpler, you just have to create a gateway and change the configuration file according to your requirements and magic will happen on its own

Conclusion:

As applications grow and evolve, so do their complexities. Microservices offer a modular approach, but they also introduce challenges. Express API Gateway, combined with the power of Node.js, offers a scalable, maintainable, and efficient way to manage, monitor, and route requests in a microservice-based architecture. Embrace the gateway, and let your microservices shine!

--

--