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 #114

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 #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:
tomhallam commented 5 years ago

@dougmoscrop please see latest PR. Have resolved the issues mentioned in the previous one, which can now be deleted.

dougmoscrop commented 5 years ago

Thanks, seems simple and useful, I don't think it will solve everyones problems (especially when building URLs programatically) but it's a good start and non-invasive!

tomhallam commented 5 years ago

Great. Scratched one of my itches with the library so hopefully it'll help others.