Skip to content

Adding urlRewrite hook

Compare
Choose a tag to compare
@jkyberneees jkyberneees released this 02 Feb 18:25
· 85 commits to master since this release

We added a new urlRewrite hook that simplifies URL rewrites using req path or queryString params:

'use strict'

const gateway = require('../index')
const PORT = process.env.PORT || 8080

gateway({
  routes: [{
    pathRegex: '',
    prefix: '/customers/:customerId',
    target: 'http://localhost:3000',
    urlRewrite: ({ params: { customerId } }) => `/users/${customerId}`
  }]
}).start(PORT).then(server => {
  console.log(`API Gateway listening on ${PORT} port!`)
})

const service = require('restana')({})
service
  .get('/users/:id', (req, res) => res.send('Hello ' + req.params.id))
  .start(3000).then(() => console.log('Service listening on 3000 port!'))

Issue ref: #58