HTTP 响应
本节介绍云函数处理 HTTP 响应相关的用法。
本节目录
返回文本
typescript
export default async function (ctx: FunctionContext) {
return 'hello world'
}
1
2
3
2
3
返回 JSON
typescript
export default async function (ctx: FunctionContext) {
return {
code: 0,
message: 'success'
}
}
1
2
3
4
5
6
2
3
4
5
6
返回 HTML
typescript
export default async function (ctx: FunctionContext) {
ctx.response.setHeader('Content-Type', 'text/html')
return '<h1>hello world</h1>'
}
1
2
3
4
2
3
4
返回状态码
typescript
export default async function (ctx: FunctionContext) {
ctx.response.status(404)
return 'Not Found'
}
1
2
3
4
2
3
4
返回重定向
typescript
export default async function (ctx: FunctionContext) {
ctx.response.redirect('https://laf.run')
}
1
2
3
2
3
流式响应
typescript
import { createReadStream } from 'node:fs'
export default async function (ctx: FunctionContext) {
const response = ctx.response
response.chunkedEncoding = true
const stream = createReadStream('package.json')
stream.on('data', chunk => {
response.write(chunk)
})
stream.on('end', () => {
response.end()
})
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
返回 Cookie
typescript
export default async function (ctx: FunctionContext) {
ctx.response.cookie('session_id', 'abc123')
return 'hello world'
}
1
2
3
4
2
3
4