Allow app developers to configure custom Express middleware
Allow Application and Extensions developers to mount Express middleware at application level.
WORKAROUND:
Our current recommendation is to create a top-level Express application (where you can easily mount any middleware) and then mount LB4 as a component (middleware) in that application. See https://loopback.io/doc/en/lb4/express-with-lb4-rest-tutorial.html for detailed instructions.
Acceptance Criteria
- [x] A set of APIs allowing Application developers to mount custom Express middleware
- [x] A set of APIs allowing Extension developers to contribute Express middleware to target application
- [x] Documentation for both App & Extension developers
- [x] A clear guidance on how to ensure correct middleware ordering (this may require a spike)
- [x] Implementation wise, the piece invoking middleware should be implemented as part of the Sequence handling, so that errors reported by Express middleware are routed to
rejectaction. (For example, we may introduce a new sequence action to be executed beforefindRoute.)
Out of scope
- Register Express middleware per route, controller class or controller method - see #2035
@hacksparrow , is this the same as #1253?
Related: https://github.com/strongloop/loopback-next/issues/559#issuecomment-399077339
I am proposing to enhance RestServer/RestApplication with an API allowing consumers to mount static files, for example
app.static(path, options). This API should leverage Express middleware server-static as an implementation detail.With this new API in place, we can either write an API Explorer component bundling swagger-ui and calling
app.static(pathToSwaggerUI), or preferably write a short guide explaining LB4 users how to add swagger-ui to their project themselves.
IMO, this is out of 4.0 GA scope as we have defined it. Relabeling this story as post-GA.
Simplifying usage is probably out of scope of GA but I think we should at the very least have some documentation around how a user can add some express middleware to their application (static file serving for example) by overriding the default requestHandler. -- I think that's something important for GA.
I feel the issue description is out of sync with the issue title. I am going to rewrite the description to match the title and focus on LB4 user experience.
Below is the original description for posterity.
Since we have decided to use Express as our underlying HTTP framework, @loopback/rest and @loopback/http-server can be refactored with the use of Express APIs.
Current Behavior
Code is not optimized to use Express APIs.
Expected Behavior
Use Express APIs and remove redundant code.
Acceptance Criteria
All HTTP stuff should be handled using Express API, wherever possible.
- Our
parseParamlogic should be kept - When an express middleware has already parsed the JSON body, our parseParam should detect this and accept the parsed JSON body from the express middleware
- redesign our sequence so that express middleware could handle errors thrown by our controllers (
reject) - docs, blogs, etc.
Hi @bajtos @dhmlau, After reading this issue, here is a spike roadmap I came up with:
- Check the need of refactoring @loopback/rest and @loopback/http-server to be used with Express API (if still relevant).
- Where to register Express middleware per route, controller class or controller method (#2035).
- Invoke middleware handling from LB Sequence.
- Clear guidance on how to ensure correct middleware ordering.
What do you think?
@orshlom your plan looks reasonable to me 👍
Personally, I think we can leave #2035 out of scope of this initial spike, but I can see how it may be useful to keep it in consideration to keep a better overall picture.
Relevant work: https://github.com/strongloop/loopback-next/pull/2434
I build a loopback component that lets you inject express middleware into the sequence. You can filter it in controllers by using the @middleware('auth') annotation, or you can directly inject middleware into the @middleware(myExpressMiddleware) annotation.
https://github.com/codejamninja/lb4-middleware
Done.
See https://loopback.io/doc/en/lb4/Middleware.html https://loopback.io/doc/en/lb4/Express-middleware.html