ctx.res.headers
Plain object snapshot of response headers (header names normalized to lowercase).
app.use((ctx) => {
console.log(ctx.res.headers) // -> { 'content-type': 'application/json' }
})
ctx.res.headers=
Replace response headers with a Headers
instance, plain object, or array of entries.
app.use((ctx) => {
ctx.res.headers = {
'content-type': 'application/json',
'cache-control': 'no-cache'
}
// or
ctx.res.headers = new Headers({
'content-type': 'application/json',
'cache-control': 'no-cache'
})
// or
ctx.res.headers = [
['content-type', 'application/json',
['cache-control', 'no-cache']
]
})
ctx.res.get(field)
Retrieve a response header by name (case-insensitive).
app.use((ctx) => {
console.log(ctx.res.get('content-type')) // -> "application/json"
})
ctx.res.getSetCookie()
Return all Set-Cookie
headers as an array.
app.use((ctx) => {
console.log(ctx.res.getSetCookie()) // -> ["session=abc", "theme=dark"]
})
ctx.res.has(field)
Check for the existence of a response header.
app.use((ctx) => {
console.log(ctx.res.has('content-type')) // -> false
})
ctx.res.set(field, value)
Set a single response header value.
app.use((ctx) => {
ctx.res.set('X-Foo', 'foo')
})
ctx.res.set(headers)
Set multiple headers from a plain object.
app.use((ctx) => {
ctx.res.set({
'X-Foo': 'foo',
'X-Bar': 'bar'
})
})
ctx.res.append(field, value)
Append a header value without removing existing ones.
app.use((ctx) => {
ctx.res.append('Set-Cookie', 'one=1; Path=/')
ctx.res.append('Set-Cookie', 'two=2; Path=/')
})
ctx.res.append(headers)
Append multiple headers using a plain object.
app.use((ctx) => {
ctx.res.append({
'X-Foo': 'foo',
'X-Bar': 'bar'
})
})
ctx.res.delete(field)
Delete a response header.
app.use((ctx) => {
ctx.res.delete('content-length')
})
ctx.res.status
Current response status code (defaults to 200
).
app.use((ctx) => {
console.log(ctx.res.status) // -> 200
})
ctx.res.status=
Set the response status code.
app.use((ctx) => {
ctx.res.status = 201
ctx.res.body = 'Created'
})
ctx.res.statusText
Current response status text. Defaults to the standard message for the current status.
app.use((ctx) => {
console.log(ctx.res.statusText) // "OK"
})
ctx.res.statusText=
Override the response status text.
app.use((ctx) => {
ctx.res.status = 202
ctx.res.statusText = 'Accepted for processing'
})
ctx.res.body
Get the current response body.
app.use(async (ctx, next) => {
console.log(ctx.res.body) // -> any
})
ctx.res.body=
Set the response body. Supports string
, object
, Blob
, ArrayBuffer
, TypedArray
, ReadableStream
, FormData
, URLSearchParams
, or Response
. Setting null
or undefined
clears the body and adjusts headers for empty responses.
app.use((ctx) => {
// string
ctx.res.body = 'Hello, Hoa!'
// json
ctx.res.body = { message: 'Hello, Hoa!' }
// Blob
ctx.res.body = new Blob(['Hello, Hoa!'])
// ArrayBuffer
ctx.res.body = new TextEncoder().encode('Hello, Hoa!').buffer
// TypedArray
ctx.res.body = new TextEncoder().encode('Hello, Hoa!')
// ReadableStream
ctx.res.body = new ReadableStream({
start (controller) {
controller.enqueue(new TextEncoder().encode('Hello, Hoa!'))
controller.close()
}
})
// FormData
const form = new FormData()
form.append('message', 'Hello, Hoa!')
ctx.res.body = form
// URLSearchParams
ctx.res.body = new URLSearchParams({ message: 'Hello, Hoa!' })
// Response
ctx.res.body = new Response('Hello, Hoa!')
// null
ctx.res.body = null
// undefined
ctx.res.body = undefined
})
ctx.res.redirect(url)
Perform an HTTP redirect (defaults to status 302
unless already a redirect status).
app.use((ctx) => {
ctx.res.redirect('https://example.com/login')
})
ctx.res.back([alt])
Redirect to the Referrer
header when safe; fallback to alt
or /
.
app.use((ctx) => {
ctx.res.back()
ctx.res.back('/home')
})
ctx.res.type
Content-Type without parameters.
app.use((ctx) => {
console.log(ctx.res.type) // -> "application/json"
})
ctx.res.type=
Set the response type and update the Content-Type
header.
app.use((ctx) => {
ctx.res.type = 'json' // Optional
ctx.res.body = { ok: true }
})
ctx.res.length
Content-Length in bytes. Automatically computed from the body when possible.
app.use((ctx) => {
ctx.res.body = 'Hello, Hoa!'
console.log(ctx.res.length) // -> 11
})
ctx.res.length=
Manually set the response Content-Length header.
app.use((ctx) => {
ctx.res.body = 'Hello'
ctx.res.length = new TextEncoder().encode(ctx.res.body).length
})
ctx.res.toJSON()
Return JSON representation of the response.
app.use((ctx) => {
console.log(ctx.res.toJSON()) // -> { status: 200, statusText: 'OK', headers: {...} }
})