vapor-docs/3.0/docs/routing/async.md

762 B

Asynchronous routing

The AsyncRouter protocol can be applied on top of any router without additional implementation.

let router: AsyncRouter = ...

Registering a route

The on function on a AsyncRouter registers a route to the provided path. The following registers a GET /hello/world route.

It responds with "Hello world!"

router.on(.get, to: "hello", "world") { request in
  return "Hello world!"
}

The .get represents the Method you want to use. to: "hello", "world" registers the path /hello/world.

The trailing closure receives a Request. The route can throw errors and needs to return a Future<ResponseRepresentable> conforming type.