gpt4 book ai didi

javascript - Express 的 res.end() 是如何工作的?

转载 作者:搜寻专家 更新时间:2023-11-01 00:32:02 25 4
gpt4 key购买 nike

docs真的不多说。

Ends the response process. Inherited from Node's http.ServerResponse.

Use to quickly end the response without any data. If you need to respond with data, instead use methods such as res.send() and res.json().

res.end();
res.status(404).end();
  1. 那么有没有东西被发送给客户?我感觉到状态行和标题已发送,但没有发送正文。否则他们为什么要给出 res.status(404).end() 的例子。

  2. 如果您向 res.end() 提供参数,它会像 Node 的 response.end() 一样在响应主体中发送您提供的参数吗? ?

最佳答案

当你发回信息时,你必须结束信息流。您可以通过调用 end() 来完成此操作。发送状态码404后仍然可以跟内容,比如自定义404页面,而不是浏览器默认给你显示This page cannot be found (Old, but an example of Chrome's built-in 404 page)。如果不结束请求,浏览器会一直等待流结束,并在一定时间后报超时。

res.send() 自动为您关闭请求,因此您不必手动调用 end()

关于javascript - Express 的 res.end() 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28289905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com