node.js express 中 res.send 和 res.write 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44692048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between res.send and res.write in express?
提问by P G
I am a beginner to express.jsand I am trying to understand the difference between res.sendand res.write?
我是一个初学者express.js,我试图理解之间的差异res.send和res.write?
回答by Omal Perera
res.send
重新发送
- res.send is only in Express js.
- Performs many useful tasks for simple non-streaming responses.
- Ability to automatically assigns the Content-Length HTTP response header field.
- Ability to provides automatic HEAD & HTTP cache freshness support.
Practical explanation
res.sendcan only be called once, since it is equivalent tores.write+res.end()Example
app.get('/user/:id', function (req, res) { res.send('OK'); });
- res.send 仅在 Express js 中。
- 为简单的非流式响应执行许多有用的任务。
- 能够自动分配 Content-Length HTTP 响应头字段。
- 能够提供自动 HEAD 和 HTTP 缓存新鲜度支持。
实战解说
res.send只能调用一次,因为它等价于res.write+res.end()例子
app.get('/user/:id', function (req, res) { res.send('OK'); });
for more details expressjs.com/en/api.html
了解更多详情expressjs.com/en/api.html
res.write
资源写入
- Can be called multiple times to provide successive parts of the body.
Example
response.write('<html>'); response.write('<body>'); response.write('<h1>Hello, World!</h1>'); response.write('</body>'); response.write('</html>'); response.end();
- 可以多次调用以提供身体的连续部分。
例子
response.write('<html>'); response.write('<body>'); response.write('<h1>Hello, World!</h1>'); response.write('</body>'); response.write('</html>'); response.end();
For more details
nodejs.org/docs
nodejs.org/en/docs/guides
回答by Keerti
res.sendis equivalent to res.write + res.endSo the key difference is res.sendcan be called only once where as res.writecan be called multiple times followed by a res.end.
res.send等价于res.write + res.end所以关键区别是res.send只能调用一次,其中 asres.write可以多次调用,后跟 a res.end。
But apart from that res.sendis part of Express. It can automatically detect the length of response header.
But there may be be a chance of memory spike with res.send(), in case of large files, our application hangs in between .
但除此之外,这res.send是 Express 的一部分。它可以自动检测响应头的长度。但是 res.send() 可能会出现内存峰值,如果文件很大,我们的应用程序会在 .send() 之间挂起。
回答by Charlie
One of the most important differencesnot indicated in any of the answers are "draining".
任何答案中都没有指出的最重要的区别之一是“排水”。
The res.writemay return true or false. As of the documentation:
该res.write可以返回true或false。根据文档:
Returns true if the entire data was flushed successfully to the kernel buffer. Returns false if all or part of the data was queued in user memory. 'drain' will be emitted when the buffer is free again.
如果整个数据已成功刷新到内核缓冲区,则返回 true。如果所有或部分数据已在用户内存中排队,则返回 false。'drain' 将在缓冲区再次空闲时发出。
So, when doing res.write, the caller should hold off writing until the drain event emits if the res.writereturned false.
因此,在执行 时res.write,调用者应推迟写入,直到res.write返回 false 时排出事件发出。
All these are handled automatically in res.send. The trade off is the buffering you will have to do when using the latter.
所有这些都在res.send. 权衡是使用后者时必须进行的缓冲。

