0
すべてのファイルを書き込んだ後に応答を終了しようとしていますが、すべての書き込みが終了する前にres.endを呼び出しています。 どのように私はそれを修正することができます、すべての書き込みを行うには、その後res.end()を呼び出しますか?res.end()の前にすべてのデータをres.writeすると、
エラーMSGの気圧:エラー:終了後に書き込み
readstream
が行われたときは、
next
を呼び出す必要が
app.get('/api/upload', function (req, res) {
gfs.files.find({}).toArray(function (err, files) {
if (files.length === 0) {
return res.status(400).send({
message: 'File not found'
});
}
res.writeHead(200, {'Content-Type': files[0].contentType});
var i = 0;
async.each(files, function (file, next) {
file.position = i;
var readstream = gfs.createReadStream({
filename: file.filename
});
readstream.on('data', function (data) {
res.write(data);
});
readstream.on('error', function (err) {
console.log('An error occurred!', err);
throw err;
});
i++;
next();
}, function (err) {
res.end();
});
});
});