Skip to content

文档改善:ctx.set (HTTP Header) #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: docs
Choose a base branch
from

Conversation

zhangciwu
Copy link

加入说明性文档,添加MDN链接,加入一些tips,去除测试语句

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
加入说明性文档,添加MDN链接,加入一些tips,去除测试语句

```js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥删掉这段?

除了 `状态码` 和 `响应体` 外,还可以通过响应 `Header` 设置一些扩展信息。
设置应答包的 `HTTP Header`

关于`HTTP Header`本身,可以查看[MDN的HTTP headers文档](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers)\
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

中文英文排版要有空格,参考下 https://github.com/sparanoid/chinese-copywriting-guidelines

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一段感觉没必要加,HTTP 协议是基础知识。

@@ -342,10 +342,17 @@ it('should response json', () => {

### `ctx.set(name, value)`

除了 `状态码` 和 `响应体` 外,还可以通过响应 `Header` 设置一些扩展信息。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这句不要干掉吧,应答 这个词很怪。直接 Header 改为 Header 接口

关于`HTTP Header`本身,可以查看[MDN的HTTP headers文档](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers)\
每个`HTTP Header`通常说明应答的资源本体(HTTP body)的属性,或者控制浏览器对该资源的一些行为(如缓存等)

> egg本身会自动添加必要的`header`(比如返回json时自动设置 `content-type`为 `application/json; charset=utf-8`,说明该资源为json类型和utf-8字符编码),另外public路径下的文件也会自动加入缓存时间(参考[egg-static](https://github.com/eggjs/egg-static))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一段也没必要


> egg本身会自动添加必要的`header`(比如返回json时自动设置 `content-type`为 `application/json; charset=utf-8`,说明该资源为json类型和utf-8字符编码),另外public路径下的文件也会自动加入缓存时间(参考[egg-static](https://github.com/eggjs/egg-static))

如果需要便捷设置资源类型的header(`content-type`),可以查看下面的 `ctx.type` 文档
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个也必要在这里,下面的 ctx.type 有提到即可。


- `ctx.set(key, value)`:可以设置一个 `Header`。
- `ctx.set(headers)`:可以同时设置多个 `Header`
- `ctx.set(headers)`:可以同时设置多个 `Header`,多个header使用对象的方式组合,如`{'key1':'value1', 'key2':'value2'}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不用,在下面的代码示例里面加一个 Object 示例即可

@TuKun33
Copy link

TuKun33 commented Mar 17, 2025

这个文档不更新了吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants