boot()
function.
Handlers are defined with the .on(method:at:handler:)
function, which takes an HTTPMethod
, a path, and a handler. The handler is a closure that accepts a Request
and returns a type that conforms to ResponseConvertable
.
There’s sugar for registering handlers for specific methods via get()
, post()
, put()
, patch()
, etc.
Response
, something conforming to ResponseConvertible
, something conforming to Encodable
, or Void
.
Response
ResponseConvertible
Encodable
Void
Response
.
Generic errors will result in an Response
with a status code of 500, but if any error that conforms to ResponseConvertible
is thrown, it will be converted as such.
Out of the box, Alchemy provides HTTPError
which conforms to ResponseConvertible
. If it is thrown, the response will contain the status code & message of the error.
setErrorHandler
to return a custom Response
whenever your app encounters an unhandled error.
notFoundHandler
to return a custom Response
when a request is made that doesn’t match any of your app’s routes.
:
). The value of the parameter can be accessed on the Request
object.
.group()
to add middleware or path prefixes to a specific group of routes.
.group()
that will isolate the prefix or middleware to any handlers defined in the closure. Using the closure based version, the example above would look like so:
Request
before a route handler is called. If the request is uploading a large file, this could balloon memory. If you’d like to enable request streaming, pass the .stream
to a handler’s options
parameter. This will call the handler as soon as the request’s body begins streaming so you can handle each chunk as it comes in.