Page Contents

Working with Express middleware

Under the hood, LoopBack leverages Express framework and its concept of middleware. To avoid common pitfalls, it is not possible to mount Express middleware directly on a LoopBack application. Instead, LoopBack provides and enforces a higher-level structure.

In a typical Express application, there are four kinds of middleware invoked in the following order:

  1. Request-preprocessing middleware like cors or body-parser.
  2. Route handlers handling requests and producing responses.
  3. Middleware serving static assets (files).
  4. Error handling middleware.

In LoopBack, we handle the request in the following steps:

  1. The built-in request-preprocessing middleware is invoked.
  2. The registered Sequence is started. The default implementation of findRoute and invoke actions will try to match the incoming request against the following resources:
    1. Native LoopBack routes (controller methods, route handlers).
    2. External Express routes (registered via mountExpressRouter API)
    3. Static assets
  3. Errors are handled by the Sequence using reject action.

Let’s see how different kinds of Express middleware can be mapped to LoopBack concepts:

Request-preprocessing middleware

At the moment, LoopBack does not provide API for mounting arbitrary middleware, we are discussing this feature in issues #1293 and #2035. Please up-vote them if you are interested in using Express middleware in LoopBack applications.

All applications come with cors enabled, this middleware can be configured via RestServer options - see Customize CORS.

While it is not possible to add additional middleware to a LoopBack application, it is possible to mount the entire LoopBack application as component of a parent top-level Express application where you can add arbitrary middleware as needed. You can find more details about this approach in Creating an Express Application with LoopBack REST API

Route handlers

In Express, a route handler is a middleware function that serves the response and does not call next(). Handlers can be registered using APIs like app.get(), app.post(), but also a more generic app.use().

In LoopBack, we typically use Controllers and Route handlers to implement request handling logic.

To support interoperability with Express, it is also possible to take an Express Router instance and add it to a LoopBack application as an external router - see Mounting an Express Router. This way it is possible to implement server endpoints using Express APIs.

Static files

LoopBack provides native API for registering static assets as described in Serve static files. Under the hood, static assets are served by serve-static middleware from Express.

The main difference between LoopBack and vanilla Express applications: LoopBack ensures that static-asset middleware is always invoked as the last one, only when no other route handled the request. This is important for performance reasons to avoid costly filesystem calls.

Error handling middleware

In Express, errors are handled by a special form of middleware, one that’s accepting four arguments: err, request, response, next. It’s up to the application developer to ensure that error handler is registered as the last middleware in the chain, otherwise not all errors may be routed to it.

In LoopBack, we use async functions instead of callbacks and thus can use simple try/catch flow to receive both sync and async errors from individual sequence actions. A typical Sequence implementation then passes these errors to the Sequence action reject.

You can learn more about error handling in Handling errors.