Page Contents

Home > @loopback/rest > MiddlewareSequence

MiddlewareSequence class

A sequence implementation using middleware chains

Signature:

export declare class MiddlewareSequence implements SequenceHandler 

Implements: SequenceHandler

Constructors

Constructor Modifiers Description
[(constructor)(context, invokeMiddleware, options)](/doc/en/lb4/apidocs.rest.middlewaresequence._constructor_.html) Constructor: Injects `InvokeMiddleware` and `InvokeMiddlewareOptions`

Properties

Property Modifiers Type Description
[defaultOptions](/doc/en/lb4/apidocs.rest.middlewaresequence.defaultoptions.html) `static` InvokeMiddlewareOptions
[invokeMiddleware](/doc/en/lb4/apidocs.rest.middlewaresequence.invokemiddleware.html) `readonly` InvokeMiddleware
[options](/doc/en/lb4/apidocs.rest.middlewaresequence.options.html) `readonly` InvokeMiddlewareOptions

Methods

Method Modifiers Description
[handle(context)](/doc/en/lb4/apidocs.rest.middlewaresequence.handle.html) Runs the default sequence. Given a handler context (request and response), running the sequence will produce a response or an error. Default sequence executes these groups of middleware: - `cors`: Enforces `CORS` - `openApiSpec`: Serves OpenAPI specs - `findRoute`: Finds the appropriate controller method, swagger spec and args for invocation - `parseParams`: Parses HTTP request to get API argument list - `invokeMethod`: Invokes the API which is defined in the Application controller method In front of the groups above, we have a special middleware called `sendResponse`, which first invokes downstream middleware to get a result and handles the result or error respectively. - Writes the result from API into the HTTP response (if the HTTP response has not been produced yet by the middleware chain. - Catches error logs it using 'logError' if any of the above steps in the sequence fails with an error.