我是 express.js
的初学者,我想了解 res.send
和 res.write
之间的区别?
最佳答案
res.send
res.send
仅在 Express.js 中。- 为简单的非流式响应执行许多有用的任务。
- 能够自动分配
Content-Length
HTTP 响应 header 字段。 - 能够提供自动 HEAD 和 HTTP 缓存新鲜度支持。
- 实用说明
res.send
只能调用一次,因为它相当于res.write
+res.end()
- 示例:
app.get('/user/:id', function (req, res) { res.send('OK'); });
更多详情:
res.write
- 可以多次调用以提供 body 的连续部位。
- 示例:
response.write('<html>'); response.write('<body>'); response.write('<h1>Hello, World!</h1>'); response.write('</body>'); response.write('</html>'); response.end();
更多详情:
关于node.js - 在 express 中 res.send 和 res.write 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44692048/