loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

Allow app developers to configure custom Express middleware

Open hacksparrow opened this issue 8 years ago • 10 comments

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 reject action. (For example, we may introduce a new sequence action to be executed before findRoute.)

Out of scope

  • Register Express middleware per route, controller class or controller method - see #2035

hacksparrow avatar Apr 27 '18 19:04 hacksparrow

@hacksparrow , is this the same as #1253?

dhmlau avatar May 14 '18 15:05 dhmlau

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.

bajtos avatar Jun 25 '18 07:06 bajtos

IMO, this is out of 4.0 GA scope as we have defined it. Relabeling this story as post-GA.

bajtos avatar Jul 30 '18 12:07 bajtos

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.

virkt25 avatar Jul 31 '18 16:07 virkt25

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 parseParam logic 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.

bajtos avatar Nov 22 '18 14:11 bajtos

Hi @bajtos @dhmlau, After reading this issue, here is a spike roadmap I came up with:

  1. Check the need of refactoring @loopback/rest and @loopback/http-server to be used with Express API (if still relevant).
  2. Where to register Express middleware per route, controller class or controller method (#2035).
  3. Invoke middleware handling from LB Sequence.
  4. Clear guidance on how to ensure correct middleware ordering.

What do you think?

orshlom avatar Jan 22 '19 13:01 orshlom

@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.

bajtos avatar Jan 31 '19 14:01 bajtos

Relevant work: https://github.com/strongloop/loopback-next/pull/2434

bajtos avatar Mar 05 '19 13:03 bajtos

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

clayrisser avatar Jun 21 '19 09:06 clayrisser

Done.

See https://loopback.io/doc/en/lb4/Middleware.html https://loopback.io/doc/en/lb4/Express-middleware.html

raymondfeng avatar May 12 '20 16:05 raymondfeng