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

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


export interface Middleware extends GenericInterceptor<MiddlewareContext> 


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;