Create API Gateway with fast-gateway
chauhoangminhnguyen

chauhoangminhnguyen @chauhoangminhnguyen

About: Software Engineer

Joined:
Jan 31, 2024

Create API Gateway with fast-gateway

Publish Date: Sep 7 '24
53 4

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:

Comments 4 total

  • Industry Connect
    Industry ConnectSep 7, 2024

    Create Production-Ready Apps in minutes : Join the Cosmocloud Low-Code Hackathon!

    Register now: whereuelevate.com/drills/cosmoclou...

  • velu-murugesan
    velu-murugesanSep 9, 2024

    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.

    • chauhoangminhnguyen
      chauhoangminhnguyenSep 10, 2024

      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).
    • Andhana Utama
      Andhana UtamaSep 10, 2024

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

Add comment