lambda-api icon indicating copy to clipboard operation
lambda-api copied to clipboard

Middlewares not being called when using wildcard "*"

Open rodrigoreis22 opened this issue 5 years ago • 4 comments

Lambda api: 0.10.1

I have this API:

const api = require('lambda-api')({base: 'v2'})
  , middleware = require('../utils/middleware')
  , LiveblogApi = require('../apis/liveblogApi');

//apply middlewares
api.use(middleware.addCorsHeaders);
api.use(middleware.logRequest);
api.use(['/data/*'], middleware.authorize);
api.use(middleware.populateCommonHeaders);
api.use(middleware.errorHandler);

api.post('/data/pbp/:eventId', async (req, res) => {
  let response = await LiveblogApi.addPost(req.params.eventId, req.body);
  return res.json(response);
});

api.post('/data/pbp/:eventId/:key', async (req, res) => {
  let response = await LiveblogApi.setPost(req.params.eventId, req.params.key, req.body);
  return res.json(response);
});

api.delete('/data/pbp/:eventId/:key', async (req, res) => {
  let response = await LiveblogApi.deletePost(req.params.eventId, req.params.key);
  return res.json(response);
});

api.post('/data/drafts/:eventId', async (req, res) => {
  let response = await LiveblogApi.addDraft(req.params.eventId, req.body);
  return res.json(response);
});

api.post('/data/drafts/:eventId/:key', async (req, res) => {
  let response = await LiveblogApi.setDraft(req.params.eventId, req.params.key, req.body);
  return res.json(response);
});

api.delete('/data/drafts/:eventId/:key', async (req, res) => {
  let response = await LiveblogApi.deleteDraft(req.params.eventId, req.params.key);
  return res.json(response);
});

api.delete('/data/comments/:eventId/:postId/:key', async (req, res) => {
  let response = await LiveblogApi.deleteComment(req.params.eventId, req.params.postId, req.params.key);
  return res.json(response);
});

module.exports = api;

You can see that I have another JS module that holds all middlewares. When I deploy it like this, it looks like only the authorize middleware is being called.

If I remove the line: api.use(['/data/*'], middleware.authorize);

Then the other middlewares are called normally.

In old versions of the lib this didn't happen, I just updated to the newest version and deployed again.

My workaround: api.use(middleware.authorize); which has the same effect as before.

rodrigoreis22 avatar May 23 '19 23:05 rodrigoreis22

Thanks for the report. The middleware execution handling was changed in v0.10 to support additional functionality. It should have been backwards compatible, but this looks like it slipped through.

jeremydaly avatar Jun 10 '19 10:06 jeremydaly

Experiencing the same problem. It works if I make the middleware non path specific and use conditional in the middleware to test the route itself.

if (/pathimlookingfor/.test(req.route)) {
   .. do something ...
}
next();

lunarwtr avatar Jun 18 '20 21:06 lunarwtr

Seems to experience a similar problem with a middleware declare on /path/:id/*:

api.use(corsMiddleware);
api.use(['/resource/:id/*', '/sub'], callerMiddleware);

api.get('/sub', postHandler);
api.post('/resource/:id/sub', getHandler);

corsMiddleware is only executed on GET, but not POST.

VincentClair avatar Mar 03 '21 13:03 VincentClair

I'm actually seeing an additional problem regarding this using a base path wildcard /*. I printed the req.stack to see what was going on with the execution stack:

const my_middleware: Middleware = (req, res, next) => {
  console.log('in my_middleware - req.stack:', req.stack)
  next()
}

const my_handler: HandlerFunction = (req, res) => {
  console.log('in my_handler    - req.stack:', req.stack)
  return res.send('done')
}

const api = require('lambda-api')()
api.use(my_middleware)
api.post('/*', my_handler)

The output:

in my_middleware - req.stack: [ 'my_middleware', 'my_middleware', 'my_handler' ]
in my_middleware - req.stack: [ 'my_middleware', 'my_middleware', 'my_handler' ]  // this is an invalid run
in my_handler    - req.stack: [ 'my_middleware', 'my_middleware', 'my_handler' ]

The middleware is being added (and run) twice.

codyfyi avatar May 28 '21 05:05 codyfyi