Skip to content
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 application #2396

Closed
ghost opened this issue Feb 14, 2019 · 4 comments
Closed

Register Express middleware per application #2396

ghost opened this issue Feb 14, 2019 · 4 comments

Comments

@ghost
Copy link

ghost commented Feb 14, 2019

As a user migrating from Express, or simply wishing to leverage an existing Express middleware in lb4 application, I would like to enable this middleware to be executed before/after lb4 route.

Consider the following definition in an Express app:

// before route 
app.use(function (req, res, next) {
  console.log('Time:', Date.now())
  next()
})

app.get('/api/info', 
  function getInfo(req, res, next) {
    // the route
  });

// after route 
app.use(function (req, res, next) {
  console.log('Time:', Date.now())
  next()
})

loopback 4 currently not support before/after route/requestHandler middleware.

I use the following short term workaround to support after route middleware

export async function setupMiddleware(restApp: Application): Promise<void> {
    const expressApp = restApp.restServer.requestHandler;
    (<express.Application>expressApp).use(async (req,res,next)=>{
        // ...
        next();
    });
}
@ghost
Copy link
Author

ghost commented Feb 14, 2019

just an idea :
middleware directory that holding all middlewares classes each class should have PreRoute and postRoute method (PreRoute before invoke controller , postRoute after invoke the controller).
with keeping in minds the order of the middleware.

class MyMiddleWare{
 readonly order =  1;
  preRoute(req,res,next){
    next();
  }
  postRoute(req,res,next){
    next(); 
  }
}

@ghost
Copy link
Author

ghost commented Feb 14, 2019

@bajtos following our discussion #2035

lets discuss high level first ,
i think loopback should support this feature

@ghost ghost changed the title Application-wide middleware Register Express middleware per application Feb 14, 2019
@bajtos
Copy link
Member

bajtos commented Mar 5, 2019

Thank you @sanadHaj for the comments. We already have a different GH issue opened to keep track of this feature - see #1293, let's close this one as a duplicate. Feel free to re-post your comments above to #1293.

Please note that as a temporary workaround, you can mount the LB4 app on a new Express instance and then register the app-wide middleware on that Express instance. See #1982 and https://loopback.io/doc/en/lb4/express-with-lb4-rest-tutorial.html.

@bajtos bajtos closed this as completed Mar 5, 2019
@bajtos
Copy link
Member

bajtos commented Mar 5, 2019

I would like to enable this middleware to be executed before/after lb4 route.

#1293 is for executing middleware before LB4 routing kicks in.

We want to allow developes to register routing middleware handling additional non-LB4 routes too, see #2389 (already in progress).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant