RoutesBuilder

A router is the logic in an application that decides who, that is, which controller, gets to respond to a request. Vapor's approach is to have the RoutesBuilder protocol, which makes every conforming class a routing class. This is very powerful and – more importantly – very performant. HTTP supports nine different kinds of requests: GET, POST, OPTIONS, DELETE, PUT, HEAD, PATCH, COPY, and SEARCH. For us, the following four will be the most relevant: GET, POST, DELETE, and PUT.

Take a look at this code example:

public func routes(_ app: Application) throws { // Example 1 app.get("hello") { req in return "Hello, world!" } // Example 2 let someController = SomeController() app.get("models", use: comeController.index) app.post("models", ...

Get Hands-On Swift 5 Microservices Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.