BackendStack21/fast-gateway

how to management not available route

Closed this issue · 2 comments

Hi, thanks for your work.
When a route is not available I get an http status 503 (Service Unavailable). Is it possible to define a hook or middleware to access this response?

Hi @maxmartinezc thanks for liking the project.

Unfortunately, the 503 response code is coming from the low level fast-proxy library and it is not configurable through options.
However, you can just use a custom middleware to intercept and post-process any response such as this 503, please see the entire example below:

'use strict'

const gateway = require('fast-gateway')
const PORT = process.env.PORT || 8080

const middleware503to404 = (req, res, next) => {
  const end = res.end
  res.end = function (...args) {
    if (res.statusCode === 503) {
      res.statusCode = 404
    }
    return end.apply(res, args)
  }

  return next()
}

gateway({
  routes: [{
    prefix: '/service',
    target: 'http://127.0.0.1:3000',
    middlewares: [
      middleware503to404
    ]
  }]
}).start(PORT).then(server => {
  console.log(`API Gateway listening on ${PORT} port!`)
})

Middlewares can be applied per route or globally. Just in case you would like to enable this behaviour for all routes.

Regards

Great, it works. Thanks for your help.

Regards