Context
Overview
Section titled “Overview”The context object gives you everything you need to handle HTTP requests:
(ctx) => { const userId = ctx.param("id"); const name = await ctx.input("name"); ctx.cookies.set("lastVisit", new Date());
return Response.json({ userId, name });}
Request Information
Section titled “Request Information”ctx.url
Section titled “ctx.url”A URL
object containing the full request URL with parsed components.
(ctx) => { console.log(ctx.url.hostname); // "localhost" console.log(ctx.url.pathname); // "/api/users" console.log(ctx.url.search); // "?page=1&limit=10"}
ctx.request
Section titled “ctx.request”The full request object containing all HTTP request details including method, headers, and body parsing utilities.
(ctx) => { console.log(ctx.request.method); // "POST" console.log(ctx.request.pathname); // "/api/users" console.log(ctx.request.ip); // "127.0.0.1"}
Headers
Section titled “Headers”ctx.headers
Section titled “ctx.headers”Access request headers through the GamanHeaders
instance.
(ctx) => { const contentType = ctx.headers.get("content-type"); const userAgent = ctx.headers.get("user-agent");}
ctx.header(key)
Section titled “ctx.header(key)”Get a specific header value by name (case-insensitive).
(ctx) => { const auth = ctx.header("authorization"); const accept = ctx.header("Accept");}
Route Parameters
Section titled “Route Parameters”ctx.param(name)
Section titled “ctx.param(name)”Get a single route parameter by name.
// Route: /users/:id(ctx) => { const userId = ctx.param("id"); // "123"}
ctx.params
Section titled “ctx.params”Get all route parameters as an object.
// Route: /posts/:postId/comments/:commentId(ctx) => { const { postId, commentId } = ctx.params; // { postId: "123", commentId: "456" }}
Query Parameters
Section titled “Query Parameters”ctx.query
Section titled “ctx.query”Access URL query parameters. Supports both function call and object access.
// URL: /search?q=hello&page=2&tags=js&tags=web(ctx) => { const search = ctx.query("q"); // "hello" const page = ctx.query.page; // "2" const tags = ctx.query.tags; // ["js", "web"]}
Request Body
Section titled “Request Body”ctx.text()
Section titled “ctx.text()”Read the request body as plain text.
(ctx) => { const textData = await ctx.text(); console.log(textData); // "Hello, world!"}
ctx.json()
Section titled “ctx.json()”Parse the request body as JSON with optional typing.
(ctx) => { const data = await ctx.json(); const user = await ctx.json<{ name: string; email: string }>();}
ctx.formData()
Section titled “ctx.formData()”Parse form data from the request body.
(ctx) => { const form = await ctx.formData(); const name = form.get("name")?.asString(); const file = form.get("avatar")?.asFile();}
ctx.input(name)
Section titled “ctx.input(name)”Get a specific form field value directly.
(ctx) => { const username = await ctx.input("username"); const email = await ctx.input("email");}
Alternative access:
(ctx) => { const username = await ctx.request.input("username"); const email = await ctx.request.input("email");}
Application State
Section titled “Application State”ctx.locals
Section titled “ctx.locals”Store and access request-scoped data. Perfect for sharing data between middlewares and route handlers.
// In middleware(ctx, next) => { ctx.locals.startTime = Date.now(); ctx.locals.user = { id: 1, name: "Angga" }; return next();}
// In route handler(ctx) => { const user = ctx.locals.user; const duration = Date.now() - ctx.locals.startTime;}
Cookies
Section titled “Cookies”ctx.cookies
Section titled “ctx.cookies”Manage HTTP cookies with a simple API. See the Cookies documentation for detailed usage.
(ctx) => { ctx.cookies.set("theme", "dark"); const theme = ctx.cookies.get("theme").value;
if (ctx.cookies.has("session")) { ctx.cookies.delete("session"); }}
Best Practices
Section titled “Best Practices”- Use
ctx.locals
to share data between middlewares and handlers - Always validate input data from
ctx.input()
andctx.json()
- Check for required parameters and headers before processing
- Use appropriate content type parsing (
json
,text
,formData
) - Handle file uploads with proper validation and size limits
- Store user context in
ctx.locals
after authentication
The context object provides a unified interface to all request data and application state, making it easy to build robust and maintainable web applications.