Nest: Suggestion: Exclude routes from MiddlewareBuilder #17

Created on 18 Jun 2018  路  9Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[ ] Bug report
[X] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior


Nest users have expressed a desire to exclude specific routes from being applied by the middleware consumer. Right now, paths must be hard-coded to exclude routes from being applied. Example: if you want to screen for a JWT in a header for all routes EXCEPT '/auth', there is no way to do so without hard-coding all paths.

Expected behavior


A common solution in #17 was to implement a MiddlewaresConsumer.exclude() method, wherein you can specifically exclude a route from consuming a middleware.

Example (with v4 syntax, not updated for v5):

consumer
    .apply(AuthMiddleware)
    .forRoutes({ path: '*', method: RequestMethod.ALL })
    .exclude({ path: '/auth', method: RequestMethod.ALL })

What is the motivation / use case for changing the behavior?


This would reduce developer friction, and give them more control over where to apply middlewares.

Environment


Nest version: N/A

Others:

core done 馃憦 type

Most helpful comment

Any news on this issue?

For me it doesn't correct the expected behavior.
The expected behavior is something like this:

            .apply(AuthMiddleware)
            .exclude({ path: '/status', method: RequestMethod.ALL })
            .forRoutes({ path: '/*', method: RequestMethod.ALL });

We want to have one declaration for all the routes except the ones provided inside the exclude method.

Thanks a lot guys.

All 9 comments

100% agree 馃憤

Same here, definitely needed that !

@alfirin the builder has already been updated and add now the RouteInfo that allow us as the previous version to configure the forRoutes by adding the path/method and also to exclude.

Added in v5.1.0

Any news on this issue?

For me it doesn't correct the expected behavior.
The expected behavior is something like this:

            .apply(AuthMiddleware)
            .exclude({ path: '/status', method: RequestMethod.ALL })
            .forRoutes({ path: '/*', method: RequestMethod.ALL });

We want to have one declaration for all the routes except the ones provided inside the exclude method.

Thanks a lot guys.

Agreed with @alfirin on this. We have the same use case and it doesn't seem to work correctly:

    consumer
      .apply(AuthenticationMiddleware)
      .exclude('health')
      .forRoutes('*')
$ curl --verbose localhost:8080/health
...
< HTTP/1.1 401 Unauthorized
...

For your cases, you should rather use either regexp path or with() method to pass custom routes (for example, health) to middleware and exclude paths manually.

On v6 of nest, it still does not work. I have same example, and there is no more with() method on MiddlewareConsumer and no real example how to provide some params to new Middlewares with use() method

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mishelashala picture mishelashala  路  3Comments

rlesniak picture rlesniak  路  3Comments

JulianBiermann picture JulianBiermann  路  3Comments

yanshuf0 picture yanshuf0  路  3Comments

cojack picture cojack  路  3Comments