Page Contents

Home > @loopback/express > Middleware

Middleware interface

Interface LoopBack 4 middleware to be executed within sequence of actions. A middleware for LoopBack is basically a generic interceptor that uses MiddlewareContext.


export interface Middleware extends GenericInterceptor<MiddlewareContext> 

Extends: GenericInterceptor<MiddlewareContext>


The middleware function is responsible for processing HTTP requests and responses. It typically includes the following logic.

  1. Process the request with one of the following outcome - Reject the request by throwing an error if request is invalid, such as validation or authentication failures - Produce a response by itself, such as from the cache - Proceed by calling await next() to invoke downstream middleware. When await next() returns, it goes to step 2. If an error thrown from await next(), step 3 handles the error.

  2. Process the response with one the following outcome - Reject the response by throwing an error - Replace the response with its own value - Return the response to upstream middleware

  3. Catch the error thrown from await next(). If the catch block does not exist, the error will be bubbled up to upstream middleware

The signature of a middleware function is described at Middleware. It’s very much the same as Koa middleware.


const log: Middleware = async (requestCtx, next) => {
  const {request} = requestCtx;
  console.log('Request: %s %s', request.method, request.originalUrl);
  try {
    // Proceed with next middleware
    await next();
    console.log('Response received for %s %s', request.method, request.originalUrl);
  } catch(err) {
    console.error('Error received for %s %s', request.method, request.originalUrl);
    throw err;