Create API Gateway with fast-gateway

Introduction

In this article, I will guide you on how to use fast-gateway to deploy a simple API Gateway on NodeJS along with express. The advantage of an API Gateway is that it acts as an intermediary layer to hide the rest of the system, including services, commonly used in Microservices architecture.

NodeJS API Gateway

Example Usage

This is the Microservices model after deployment:

API gateway

First, install the package

yarn add fast-gateway
Enter fullscreen mode
Exit fullscreen mode

Next, define the ports that will be used.

import * as express from 'express'
import * as gateway from 'fast-gateway' const portGateway = 5000
const portService1 = 5001
const portService2 = 5002
Enter fullscreen mode
Exit fullscreen mode

Define service 1 as follows:

const startService1 = (): void => { const app = express() app.get('/list', (req, res) => { const items = [ { id: 1, name: 'service 1 value 1', }, { id: 2, name: 'service 1 value 2', }, ] res.status(200).json(items) }) app.get('/', (req, res) => { res.send('Service 1 index') }) app.listen(portService1, () => { console.log('Service 1 running at http://localhost:' + portService1) })
}
Enter fullscreen mode
Exit fullscreen mode

The content is quite simple: I just use Express to start the service.

Service 2 is similar.

const startService2 = (): void => { const app = express() app.get('/list', (req, res) => { const items = [ { id: 1, name: 'service 2 value 1', }, { id: 2, name: 'service 2 value 2', }, ] res.status(200).json(items) }) app.get('/', (req, res) => { res.send('Service 2 index') }) app.listen(portService2, () => { console.log('Service 2 running at http://localhost:' + portService2) })
}
Enter fullscreen mode
Exit fullscreen mode

After that, just define the Gateway and start the services.

const startGateWay = (): void => { const server = gateway({ routes: [ { prefix: '/service-1', target: `http://localhost:${portService1}/`, }, { prefix: '/service-2', target: `http://localhost:${portService2}/`, }, ], }) server .get('/', (req, res) => { res.send('Gateway index') }) .get('/about', (req, res) => { res.send('Gateway about') }) server.start(portGateway).then(server => { console.log('Gateway is running at http://localhost:' + portGateway) })
} startService1()
startService2()
startGateWay()
Enter fullscreen mode
Exit fullscreen mode

In a real-world scenario, each service would typically be a separate project repository.

Services running

Next, when you access the pages http://localhost:5000/service-1/ or http://localhost:5000/service-2/, they will be forwarded to the corresponding service.

Service 1 running

Service 2 running

Happy coding!


If you found this content helpful, please visit the original article on my blog to support the author and explore more interesting content.

BlogspotBlogspotDev.toFacebookX


Some series you might find interesting:


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top