-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion: Exclude routes from MiddlewareBuilder #17
Comments
Hey @cdiaz, The idea is very cool ! |
Hi @thomrick, It's the basic way I do it with express: let except = function(path, middleware) {
return function(req, res, next) {
if (path.indexOf(req.path) > -1) {
// Exclude
return next()
}
else {
// Apply for all others
return middleware(req, res, next)
}
}
} app.use(except(['/some', '/another'], authMiddleware())); I think it can be implemented in Nest, something like this: builder.apply(authMiddleware)
.forRoutes(UsersController)
.except({
path: ['/some', '/another']
}); |
Hey @cdiaz, I looked up on the Nest documentation and I found this: When you pass UsersController in forRoutes method, Nest will setup middleware for each route in controller:
But it is also possible to directly define for which path middleware should be used, just like that: builder.apply(AuthMiddleware)
.forRoutes({ path: '*', method: RequestMethod.ALL }); It isn't be better than adding new methods ? |
@thomrick Assuming my controller has 20 routes and I need to exclude 2, it is better to write only 2 to exclude them instead of write 18 explicitly |
@cdiaz in fact you make a good point if you write these 20 routes in 1 Controller !!! But why writing 20 routes in 1 Controller ? This problem can't be solved be refining architecture / design ? |
I was referring to a hypothetical case, it all depends on the requirements. in fact I was researching and I found this package. |
Hi @cdiaz, You can just use resolve(path) {
return (req, res, next) => {
if (path.indexOf(req.path) > -1) {
next();
}
...
};
} |
For those interested, I needed similar functionality in some of my middleware, particularly my AuthModule where I needed to allow access to one route, but not the rest in a particular controller. I followed this, and wanted to come up with a more standardised way of excluding the route.
And my middleware as follows
Let me know what you think/any improvements as I am still learning. |
maybe consumer
.apply(AuthMiddleware)
.forRoutes({ path: '*', method: RequestMethod.ALL })
.exclude({ path: '/auth', method: RequestMethod.ALL }) //or `excludeRoutes` |
I agree something like that would be ideal, but @kamilmysliwiec already proposed solution.. I guess it's not a major issue at the moment |
Why not have both 🎉 |
Definitely the suggestion of @wbhob is needed @kamilmysliwiec |
@kamilmysliwiec Can you reopen this issue or do you reject the feature as suggested by @wbhob ? I agree it would be very handy. My example use case: |
Reiterating the previous comment, can this issue be reopened? |
Recreated in #790. Please continue all conversation there, and upvote if you need this functionality. |
Is it still possible exclude methods? |
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. |
In some cases, a middleware is applied to all routes of a controller, except some.
To avoid having to write all in forRoutes(), there may be a way to exclude: exceptRoutes().
It's just an idea
The text was updated successfully, but these errors were encountered: