关于nodejs中res.send和res.end如何理解

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


其实:

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容