响应

PPG007 ... 2023-5-9 Less than 1 minute

# 响应

# 响应头相关

app.use((ctx) => {
    ctx.response.set({
        reqId: '123123'
    })
    ctx.response.set('wuhu', 'wuhu');
    ctx.response.append('wuhu2', 'wuhu2');
    console.log(ctx.response.get('wuhu'));
    ctx.response.remove('wuhu');
})
1
2
3
4
5
6
7
8
9

# 响应状态相关

app.use((ctx) => {
    ctx.response.status = 200;
    // 通常情况下 message 与 status 关联
    ctx.response.message = '1!5!';
})
1
2
3
4
5

# 响应体相关

将响应体设置为以下之一:

  • string 写入。
  • Buffer 写入。
  • Stream 管道。
  • Object || Array JSON-字符串化。
  • null 无内容响应。
app.use((ctx) => {
    ctx.response.status = 200;
    ctx.response.body = {
        user: 'PPG007'
    };
})
1
2
3
4
5
6

# 重定向

app.use((ctx) => {
    ctx.response.redirect('https://baidu.com');
    // 重定向到 referrer
    // ctx.redirect('back');
})
1
2
3
4
5
Last update: May 9, 2023 09:46
Contributors: Koston Zhuang