javascript - 在node.js中,如果使用createReadStream()还需要response.end()

标签 javascript node.js

node.js doc

response.end([数据], [编码])

This method signals to the server that all of the response headers and body have been sent; that server should consider this message complete. The method, response.end(), MUST be called on each response.

但是在使用createReadStream给出的许多示例中,例如,

 fs.createReadStream(filename,{...}).pipe(response)
我从未见过 response.end() 被调用。

它需要 resonse.end()createReadStream() 一起使用吗?如果是这样,它出现在程序流中的什么位置 - 您不希望在管道完成之前调用它,对吗?

最佳答案

在读取完成(end 事件)或遇到错误(error 事件)后执行 response.end()

检查这个例子:http://elegantcode.com/2011/04/06/taking-baby-steps-with-node-js-pumping-data-between-streams/

关于javascript - 在node.js中,如果使用createReadStream()还需要response.end(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12723192/

相关文章:

javascript - 让 Discord Bot 加入/离开 channel

node.js - Google App Engine Flexible env 的定价,500 美元的类(class)

node.js - 我是否可以在 synaptics.js Node 中重用 LSTM RNN 的训练模型?

javascript - .then 应该在函数体内返回最后一个 promise 吗?

javascript - Material 自动完成不适用于 InputProps

javascript - 使用 Javascript TreeWalker 按类名称过滤以更改文本

javascript - html 和 javascript 中相同的 id 具有不同的类名

javascript - 如何在 typescript-node 项目中配置 jest-mongodb(@shelf/jest-mongodb)?

node.js - 如何在 Jest 测试中发送 Node 命令行参数

javascript - 使用 Mongoose 和 NodeJS 在 MapReduce 中添加更多字段