DEV Community

chauhoangminhnguyen
chauhoangminhnguyen

Posted on

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:

Top comments (3)

Collapse
 
velumurugesan profile image
velu-murugesan

I got cors error in my full stack application how can I solve this ,could you please help me,i don't where to check frontend or backend.

Collapse
 
chauhoangminhnguyen profile image
chauhoangminhnguyen

A CORS error occurs when you make a request to a domain/port different from the one where the frontend app is running.

  1. If it's a production app, configure the server to allow CORS.
  2. If it's in development, use an intermediary proxy (since CORS is only blocked by the browser).
Collapse
 
magicwarms profile image
Andhana Utama

hi @velumurugesan, usually the CORS issue is on Backend, you can check there.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.