-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Register Express middleware per route, controller class or controller method #2035
Comments
See #1671, which can be possibly used as the plumbing. |
@bajtos @raymondfeng @dhmlau for example this method will added to restApplication
so developer can run app.middleware(MyMiddleWare); using this approach we can make the whole thing separated and more clean . |
@raymondfeng let me know if i am right, |
Good discussion! Here is my point of view. From our experience with using Express and Express middleware in LoopBack 1.x/2.x/3.x, middleware handlers are difficult to compose and reason about, especially when there are multiple places contributing them (the application + 3rd-party extensions). We have somewhat addressed this problem with "middleware phases" (docs). Unfortunately, it seems like this concept did not gain wider adoption. For LoopBack 4, we would like to offer a different design, one that's closer to our concept of a Sequence, a Controller with methods, and a handler route. I think beforeRemote/afterRemote hooks in LoopBack 3.x are a good example of a solution that's different from Express middleware and more tailored to LoopBack programming model. This GitHub issue IS NOT about those hooks, it's not about the LB4 way of handling things. Having a custom design for before/after hooks comes with a downside too: app developers cannot reuse existing Express middleware and have to either wait until somebody ports Express middleware to LB4, or implement that functionality themselves. There are also Express users with large applications containing many routes and leveraging express middleware configured differently for each route. I would like to make it easier for them to upgrade their Express-based applications to LoopBack 4, to allow them to upgrade incrementally. The goal of this issue is to support those two groups of users:
I don't think loopback-phases (#1671) is a good solution for this particular feature. Phases may be useful for application-wide middleware, that's out of scope of this issue though (see #1293 and #1982).
This may work for LB4-specific hooks/middleware, but that's not what we want here. This GitHub issue is focused specifically on allowing LB4 routes to call Express middleware. @sanadHaj could you please open a new issue to discuss your specific scenario? It looks like something LoopBack should support, but I feel we need more details to better understand what you are trying to achieve. It's better to move the discussion to a new issue to keep the discussion here focused on the original topic. |
@bajtos @use( requireAuthenticated()) |
@sanadHaj
As I wrote before, this GH issue is about using regular Express middleware, e.g. helmet, cors or even passport. AFAIK, there is no concept of Based on your older comment:
I think you are looking for "method interceptors" that have been recently introduced by #2687, see also #2907, https://loopback.io/doc/en/lb4/Interceptors.html and https://loopback.io/doc/en/lb4/Interceptor-generator.html. |
You can register middleware on a controller using the lb4-middleware component. @middleware((_req: Request, _res: Response, next: NextFunction) => {
return next();
})
ping(): object {
return {
greeting: 'Hello from LoopBack'
};
} |
In case someone else comes here, see the comment here |
See PR #5118 |
As a user migrating from Express, or simply wishing to leverage an existing Express middleware in my application, I would like to enable this middleware for certain routes, controller methods or entire controller classes.
Consider the following definition in an Express app:
The code snippets below illustrate different ways of applying the middleware. Please note they are just examples, not a final proposal.
Per route
A possible LB4 solution (not very ergonomic, a better solution is needed):
Per controller method
Per controller class
This allows developers to apply the same set of middleware for multiple routes.
Acceptance criteria
TBD
Out of scope
app.use(passport.initialize())
- see Allow app developers to configure custom Express middleware #1293 and Tutorial/Blog: how to mount LB app as Express/Koa middleware #1982app.use((err, req, res, next) => {/*...*/})
- see Allow app developers to configure custom Express middleware #1293serve-static
- developers should useapp.static()
API.The text was updated successfully, but these errors were encountered: