跳到主要内容
CNode

问个低级问题,关于res.send和res.end

问答
Qqxl1231发布于 10 年前最后回复 9 年前
5178450

很多时候,大家都再说res.end 必须要在res.send后加上,不然客户端会一直等待回应?

但是我们在写代码的时候,很多时候直接,res.send();并没有加end,也没事.这是为什么?

以下是官方文档说明: res.end([data] [, encoding]) Ends the response process. This method actually comes from Node core, specifically the response.end() method of 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

查看回复

回复 (5)

R
royalrover#59 年前

res.end 是相对于 res.write而言的

K
kaimZhou#49 年前

@ncuzp 正解

N
ncuzp#210 年前

简单说就是如果服务端没有数据传回客户端就可以直接用red.end返回,如果有数据可以使用res.send,red.json此时可以不写res.end了,因为在前面两个方法中默认会返回

X
xinyu198736#110 年前

谁说的,揪出来。。。这个直接去看一下源码就知道了。。

参与回复

登录后即可参与回复。登录