Create API Gateway with fast-gateway



This content originally appeared on DEV Community and was authored by chauhoangminhnguyen

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

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

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)
  })
}

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)
  })
}

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()

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:


This content originally appeared on DEV Community and was authored by chauhoangminhnguyen