dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Other
1.72k stars 166 forks source link

feat: Add basePath option, to allow for virtual mount points #113

Closed tomhallam closed 5 years ago

tomhallam commented 5 years ago

I'm working with an application where we want to reason about portions of the application seperately, i.e. we have a seperate lambda for users, transactions, pictures or whatever.

This PR allows you to suggest what the "mount point"/"base path" is for the serverless application, so it knows to strip this from the incoming path.

If specified, it also strips the stage information from the path, so it can work offline and in the real world!

Fixes https://github.com/dougmoscrop/serverless-http/issues/68

BEFORE:

serverless.yml

  ## TRANSACTIONS ---------------------------------------------
  transactions:
    handler: handler.default
    events:
      - http: ANY /transactions
      - http: 'ANY /transactions/{proxy+}'

handler.js

app.get('/new', (req, res) => {
  return res.send('woop');
});
module.exports.default = serverless(app);

request:

[GET] http://localhost/transactions/new -> 404 :'(

AFTER:

app.js

app.get('/new', (req, res) => {
  return res.send('woop');
});
module.exports.default = serverless(app, {
  basePath: '/transactions'
});

request

[GET] http://localhost/transactions/new -> 200 :+1:
[GET] http://apigateway.amazon.com/dev/transactions/new -> 200 :+1: