BackendStack21 / fast-gateway

Fast-gateway is an easy to use Node.js API gateway framework built to handle large scale API traffic with great performance and scalability. Written with JavaScript, it is a framework for the masses!
MIT License
311 stars 35 forks source link

Question: can this work with express? #5

Closed clarktlaugh closed 5 years ago

clarktlaugh commented 5 years ago

I have some other routes (in express) and I want to also use fast-gateway . Is this possible? I saw where I can pass in options to fast-gateway for restana (opts.restana) and provide it a pre-existing server instance to use.

I'm doing something like this:

const app = express()
const server = http.createServer(app)

app.get('/route1', function(req, res) {
...
})

app.get('/route2', function(req, res) {
...
})

gateway({
  restana: {
    server: server
  },
  middlewares: [
    require('cors')(),
    require('helmet')()
  ],

  routes: [{
    prefix: '/gw-route1',
    target: 'http://another-host:8080'
  }, {
    prefix: '/gw-route2',
    target: 'http://another-host2:8080',
    middlewares: [
        passport.authenticate('jwt', { session: false })
    ]
  }]
}).start(PORT).then(server => {
  console.log(`API Gateway listening on port ${PORT}`)
})

My express routes are working. The ones defined in fast-gateway are not.

jkyberneees commented 5 years ago

Hi @clarktlaugh thanks for reaching out. Although I would highly recommend to run your API gateway isolated from your services logic, here I describe the way you can get your solution working:

const app = express()
app.get('/route1', function(req, res) {
  ...
})
app.get('/route2', function(req, res) {
  ...
})

gateway({
  restana: {
    defaultRoute: (req, res) => app.handle(req, res)
  },
  ...

This will route to the express application all the un-matched requests on the gateway.

Thanks and Regards.

clarktlaugh commented 5 years ago

Thank you. My intent was to add an admin API to my gateway service. But, I should consider how to do it as a separate service.