Vapor Documentation

Protocol Middleware

public protocol Middleware  

Middleware is placed between the server and your router. It is capable of mutating both incoming requests and outgoing responses. Middleware can choose to pass requests on to the next Middleware in a chain, or they can short circuit and return a custom Response if desired.

%757 Middleware Middleware AsyncMiddleware AsyncMiddleware AsyncMiddleware->Middleware CORSMiddleware CORSMiddleware CORSMiddleware->Middleware SessionsMiddleware SessionsMiddleware SessionsMiddleware->Middleware RedirectMiddleware RedirectMiddleware RedirectMiddleware->Middleware GuardAuthenticationMiddleware GuardAuthenticationMiddleware GuardAuthenticationMiddleware->Middleware RouteLoggingMiddleware RouteLoggingMiddleware RouteLoggingMiddleware->Middleware ErrorMiddleware ErrorMiddleware ErrorMiddleware->Middleware Authenticator Authenticator Authenticator->Middleware FileMiddleware FileMiddleware FileMiddleware->Middleware

Types Conforming to Middleware

Authenticator

Helper for creating authentication middleware.

AsyncMiddleware

AsyncMiddleware is placed between the server and your router. It is capable of mutating both incoming requests and outgoing responses. AsyncMiddleware can choose to pass requests on to the next AsyncMiddleware in a chain, or they can short circuit and return a custom Response if desired.

CORSMiddleware

Middleware that adds support for CORS settings in request responses. For configuration of this middleware please use the CORSMiddleware.Configuration object.

ErrorMiddleware

Captures all errors and transforms them into an internal server error HTTP response.

FileMiddleware

Serves static files from a public directory.

RouteLoggingMiddleware

Emits a log message containing the request method and path to a Request's logger. The log level of the message is configurable.

SessionsMiddleware

Uses HTTP cookies to save and restore sessions for connecting clients.

Default Implementations

make​Responder(chaining​To:​)

func makeResponder(chainingTo responder: Responder) -> Responder  

Wraps a Responder in a single Middleware creating a new Responder.

Requirements

respond(to:​chaining​To:​)

func respond(to request: Request, chainingTo next: Responder) -> EventLoopFuture<Response>

Called with each Request that passes through this middleware.

Parameters

request Request

The incoming Request.

next Responder

Next Responder in the chain, potentially another middleware or the main router.

Returns

An asynchronous Response.