{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"Middleware","type":"basic","slug":"middleware","excerpt":"","body":"Add your own middleware to the application's middleware array to filter all incoming requests and responses.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": (M\"app.globalMiddleware.append(MyMiddleware())\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nYou can also add middleware to individual routes. Visit the Routing and Middleware sections of this guide to learn more.\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"Append to middleware\",\n  \"body\": \"Make sure to append to the middleware unless your intention is to remove Vapor's default middlewares that do session and error handling.\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Creating Middleware\"\n}\n[/block]\nCustom middleware is easy to create. And Vapor's middleware is compatible with [S4](https://github.com/open-swift/S4) by default, so any middleware you create could be used in other S4 applications.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"public class MyMiddleware: Middleware {\\n\\n    public func respond(to request: Request, chainingTo chain: Responder) throws -> Response {\\n        // do things to the request here\\n        let response = try chain.respond(to: request)\\n        // do things to the response here\\n        return response\\n    }\\n\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]","updates":["57742b258029ad0e00c38fa9","5775d95eece8a00e00a15ba9"],"order":6,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"5768701b72cd4c0e000fad23","user":"57086f3d447c410e00221bd1","parentDoc":null,"project":"57086fe855a4690e005de407","version":{"version":"0.12","version_clean":"0.12.0","codename":"Client Clyde","is_stable":false,"is_beta":true,"is_hidden":false,"is_deprecated":true,"categories":["5768701b72cd4c0e000fad19","5768701b72cd4c0e000fad1a","5768701b72cd4c0e000fad1b","5768701b72cd4c0e000fad1c"],"_id":"5768701b72cd4c0e000fad18","releaseDate":"2016-06-20T22:37:15.641Z","__v":1,"createdAt":"2016-06-20T22:37:15.641Z","project":"57086fe855a4690e005de407"},"createdAt":"2016-06-13T15:58:27.993Z","__v":2,"category":{"sync":{"isSync":false,"url":""},"pages":[],"title":"Guide","slug":"getting-started","order":1,"from_sync":false,"reference":false,"_id":"5768701b72cd4c0e000fad1a","createdAt":"2016-04-09T04:31:52.489Z","__v":0,"version":"5768701b72cd4c0e000fad18","project":"57086fe855a4690e005de407"},"githubsync":""}
Add your own middleware to the application's middleware array to filter all incoming requests and responses. [block:code] { "codes": [ { "code": (M"app.globalMiddleware.append(MyMiddleware())", "language": "swift" } ] } [/block] You can also add middleware to individual routes. Visit the Routing and Middleware sections of this guide to learn more. [block:callout] { "type": "warning", "title": "Append to middleware", "body": "Make sure to append to the middleware unless your intention is to remove Vapor's default middlewares that do session and error handling." } [/block] [block:api-header] { "type": "basic", "title": "Creating Middleware" } [/block] Custom middleware is easy to create. And Vapor's middleware is compatible with [S4](https://github.com/open-swift/S4) by default, so any middleware you create could be used in other S4 applications. [block:code] { "codes": [ { "code": "public class MyMiddleware: Middleware {\n\n public func respond(to request: Request, chainingTo chain: Responder) throws -> Response {\n // do things to the request here\n let response = try chain.respond(to: request)\n // do things to the response here\n return response\n }\n\n}", "language": "swift" } ] } [/block]