To route is to tell which function should return a response.
The function for returning a Response is Handler
.
Handler is a callback function which accept Context
, helping you access powerful API for accessing data and modifying the response.
The params
from the previous example also come from Handler
Context's properties consists of
request
: Raw Request
for accessing data as web standard typebody
: Body which come with the requestquery
: Parsed path query as a simple objectparams
: Path parameters as a simple objectstore
: A global mutable store for Elysia instanceset
: Response representation
status
: response statusheaders
: response headersredirect
: redirect to new pathYou can access Context
in Handler
function:
Elysia encourages object destructuring, but set
is an exception.
As destructured primitive value is not linked to the object, in order to make set
work properly, we need to use set.value
Returning value from Handler
, Elysia will try to map returned value into Response
.
For example, to return an object, you should stringify the content first and then set response header of Content-Type
to application/json
.
Which look something like this:
But Elysia handles that for you.
You simply return an object, and Elysia will map your value to a correct response for you.
Of course, as Elysia map the value to Response
.
But you can also return a Response
if you really want to.