如何在express中流式传输响应?
如何在express中流式传输响应?
我一直在尝试让一个express应用程序以流的形式发送响应。
var Readable = require('stream').Readable; var rs = Readable(); app.get('/report', function(req,res) { res.statusCode = 200; res.setHeader('Content-type', 'application/csv'); res.setHeader('Access-Control-Allow-Origin', '*'); // 强制下载的头部 res.setHeader('Content-disposition', 'attachment; filename=Report.csv'); rs.pipe(res); rs.push("USERID,NAME,FBID,ACCOUNT,SUBSCRIPTION,PRICE,STATE,TIMEPERIOD\n"); for (var i = 0; i < 10; i++) { rs.push("23,John Doe,1234,500,SUBSCRIPITON,100,ACTIVE,30\n"); } rs.push(null); });
当我将"rs.pipe(res)"替换为"rs.pipe(process.stdout)"时,它确实在控制台上打印出来。但如何使其在express应用程序中正常工作呢?
错误:未实现 at Readable._read (_stream_readable.js:465:22) at Readable.read (_stream_readable.js:341:10) at Readable.on (_stream_readable.js:720:14) at Readable.pipe (_stream_readable.js:575:10) at line "rs.pipe(res);"