These docs are for v0.12. Click to read the latest docs for v0.16.

Middleware

Add your own middleware to the application's middleware array to filter all incoming requests and responses.

You can also add middleware to individual routes. Visit the Routing and Middleware sections of this guide to learn more.

🚧

Append to middleware

Make sure to append to the middleware unless your intention is to remove Vapor's default middlewares that do session and error handling.

Creating Middleware

Custom middleware is easy to create. And Vapor's middleware is compatible with S4 by default, so any middleware you create could be used in other S4 applications.

public class MyMiddleware: Middleware {

    public func respond(to request: Request, chainingTo chain: Responder) throws -> Response {
        // do things to the request here
        let response = try chain.respond(to: request)
        // do things to the response here
        return response
    }

}