问个低级问题,关于res.send和res.end
发布于 8 年前 作者 qxl1231 16432 次浏览 来自 问答

很多时候,大家都再说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 回复

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

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

@ncuzp 正解,果然理解深刻,我看了下确实如此

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

回到顶部