Working with Express middleware
Warning: First-class support for Express middleware has
been added to LoopBack since v4.0.0 of
@loopback/rest. Please refer to
Using Express Middleware. The following information
only applies to earlier versions of
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:
- Request-preprocessing middleware like cors or body-parser.
- Route handlers handling requests and producing responses.
- Middleware serving static assets (files).
- Error handling middleware.
In LoopBack, we handle the request in the following steps:
- The built-in request-preprocessing middleware is invoked.
- The registered Sequence is started. The default implementation of
invokeactions will try to match the incoming request against the following resources:
- Native LoopBack routes (controller methods, route handlers).
- External Express routes (registered via
- Static assets
- Errors are handled by the Sequence using
Let’s see how different kinds of Express middleware can be mapped to LoopBack concepts:
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.
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
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.post(), but also a more generic
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.
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:
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
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
You can learn more about error handling in Handling errors.