Request & response
What you read from the request, how bodies are parsed, and how responses carry types.
Request
| Property | Notes |
|---|---|
url | Normalized pathname |
method | HTTP method enum |
headers | Native Headers |
body | Parsed body (schema type, else unknown) |
query | Query params; schema type or Record<string, string | undefined> |
params | Route params; inferred from the URL or schema |
cookies | Parsed Cookie header, URI-decoded |
ip | Client IP, or null |
data | Context filled by middlewares |
raw | Original Request |
query, params, and cookies are read-only. You write to data and body.
Body parsing
Parsed from Content-Type. GET never gets a body. Malformed bodies yield null rather than throwing.
| Content-Type | req.body |
|---|---|
| application/json | Parsed JSON |
| multipart/form-data | Object of fields; files as Web File |
| application/x-www-form-urlencoded | Object of string fields |
| anything else | Raw text |
For uploads, validation with z.file(), and sendFile, see Files.
Cookies
If the request has a Cookie header, each name=value pair is stored in req.cookies. Values are passed through decodeURIComponent; if decoding fails, the raw value is kept.
.get("/me", (req, res) => {
const session = req.cookies.session;
return res.json({ session: session ?? null });
})Response
res.json({ message: "Success" }); // 200 JSON (typed for the client)
res.status(201).json({ created: true }); // custom status
res.send("Plain text"); // raw body
res.redirect("/new-location", 301); // redirect (default 307)
await res.sendFile("/path/to/file.pdf"); // stream a file
res.type("xml").send("<root></root>"); // Content-Type from extension
res.setHeader("X-Custom", "value").json({});res.json()returnsTypedResponse<T>- that phantom type is whatHttpClientreadssend/redirect/sendFilereturn a plainResponseand contribute no type informationtype(ext)maps an extension (e.g."json","pdf") to a MIME type via@std/media-typessize(n)setsContent-Length