jfromaniello/express-unless

Support for method and path combination

okjulian opened this issue · 9 comments

Is there a way to exclude specific methods from a path?

For example suppose I have a /items path, and want to skip the middleware on GET /items and run it on POST | PUT | DELETE /items.

Something like:

app.use(myMiddleware.unless({ path: { "/items": [ 'GET' ] } }));

+1, just ran into this today. For the time being I will write a custom function to achieve the desired logic, but it seems like a simple addition of another attribute.

+1

The way I accomplished this with a custom unless function:

app.use(middleware().unless(function(req) {
  return (
      req.originalUrl === '/endpoint1' && req.method === 'GET' ||
      req.originalUrl === '/endpoint2' && req.method === 'POST'
  );
}));

Hope it helps.

@goncaloneves Thanks for the code. It works, but it would be nice if it was baked in.

@chuckconway @goncaloneves feature added in v0.3

I'm using unless with express-jwt to whitelist some endpoints from having to have token, but looks like it does not respect the http method I provide

app.use(
    jwt({
      secret: process.env.JWT_SECRET
    }).unless({
       path: [
            //...
           { url: 'estimate/category', method: 'GET' },
            //...
       ]
    })

as I understand, it should whitelist 'estimate/category' just for GET method, in other words, I have to provide token if it is POST or PATCH and don't have to provide token if it's GET

but it is whitelisting it for all methods.

I think the problem is here:

skip = skip || methods.indexOf(req.method) > -1;

Shouldn't it be a && here?
This code says here if skip exists, methods are not important

Environment

I'm using:
"express": "4.17.1",
"express-jwt": "5.3.3",
"express-unless":"0.3.1"