Skip to content

Routing

Routing is the process of finding the appropriate response to an incoming request.

Making a Router

In Vapor the default Router is the EngineRouter. You can implement custom routers by implementing one conforming to the Router protocol.

let router = try EngineRouter.default()

This is usually done in your configure.swift file.

Registering a route

Imagine you want to return a list of users when someone visits GET /users. Leaving authorization aside, that would look something like this.

router.get("users") { req in
    return // fetch the users
}

In Vapor, routing is usually done using the .get, .put, .post, .patch and .delete shorthands. You can supply the path as / or comma-separated strings. We recommend comma separated, as it's more readable.

router.get("path", "to", "something") { ... }

Routes

The best place to add routes is in the routes.swift file. Use the router supplied as a parameter to this function to register your routes.

import Vapor

public func routes(_ router: Router) throws {
    // Basic "Hello, world!" example
    router.get("hello") { req in
        return "Hello, world!"
    }

    /// ...
}

See Getting Started → Content for more information about what can be returned in a route closure.

Parameters

Sometimes you may want one of the components of your route path to be dynamic. This is often used when you want to get an item with a supplied identifier, e.g., GET /users/:userID

router.get("users", Int.parameter) { req -> String in
    let userID = try req.parameters.next(Int.self)
    return "requested user id #\(userID)"
}

Instead of passing a string, pass the type of parameter you expect. In this case, our User has an Int ID.

Tip

You can define your own custom parameter types as well.

After registering your routes

After registering your routes you must register the Router as a Getting Started → Services