HBRouter
public protocol HBRouter : HBResponder, HBRouterMethods
Directs Requests to handlers based on the request uri.
Conforms to HBResponder
so need to provide its own implementation of
func apply(to request: Request) -> EventLoopFuture<Response>
.
HBRouter
requires an implementation of the on(path:method:use)
functions but because it
also conforms to HBRouterMethods
it is also possible to call the method specific functions get
, put
,
head
, post
and patch
. The route handler closures all return objects conforming to
HBResponseGenerator
. This allows us to support routes which return a multitude of types eg
app.router.get("string") { _ -> String in
return "string"
}
app.router.post("status") { _ -> HTTPResponseStatus in
return .ok
}
app.router.data("data") { request -> ByteBuffer in
return request.allocator.buffer(string: "buffer")
}
Routes can also return EventLoopFuture
‘s. So you can support returning values from
asynchronous processes.
The default Router
setup in HBApplication
is the TrieRouter
. This uses a
trie to partition all the routes for faster access. It also supports wildcards and parameter extraction
app.router.get("user/*", use: anyUser)
app.router.get("user/:id", use: userWithId)
Both of these match routes which start with “/user” and the next path segment being anything.
The second version extracts the path segment out and adds it to HBRequest.parameters
with the
key “id”.
-
Add router entry
Declaration
Swift
func add(_ path: String, method: HTTPMethod, responder: HBResponder)
-
on(_:method:options:use:)
Extension methodAdd path for closure returning type conforming to ResponseFutureEncodable
Declaration
Swift
@discardableResult public func on<Output: HBResponseGenerator>( _ path: String, method: HTTPMethod, options: HBRouterMethodOptions = [], use closure: @escaping (HBRequest) throws -> Output ) -> Self
-
on(_:method:options:use:)
Extension methodAdd path for closure returning type conforming to ResponseFutureEncodable
Declaration
Swift
@discardableResult public func on<Output: HBResponseGenerator>( _ path: String, method: HTTPMethod, options: HBRouterMethodOptions = [], use closure: @escaping (HBRequest) -> EventLoopFuture<Output> ) -> Self
-
group(_:)
Extension methodreturn new
RouterGroup
Declaration
Swift
public func group(_ path: String = "") -> HBRouterGroup
Parameters
path
prefix to add to paths inside the group