Handler and context
Handler - a resource or a route function to send data back to client.
import { Elysia } from 'elysia'
// `() => 'hello world'` is a handler
.get('/', () => 'hello world')
A handler can also be a literal value, see Handler
import { Elysia } from 'elysia'
// `'hello world'` is a handler
Using an inline value can be useful for static resources like files.
Context
Contains information about each request. It is passed as the only argument of a handler.
import { Elysia } from 'elysia'
.get('/', (context) => context.path)
Context stores information about the request like:
- body - data sent by client to server like form data, JSON payload.
- query - query string as an object. (Query is extracted from a value after pathname starting from ’?’ question mark sign)
- params - Path parameters parsed as object
- headers - HTTP Header, additional information about the request like “Content-Type”.
See Context.
Preview
You can preview the result by looking under the editor section.
There should be a tiny navigator on the top left of the preview window.
You can use it to switch between path and method to see the response.
You can also click to edit body, and headers.
Assignment
Let’s try extracting context parameters: