Nodejs发送部分响应 [英] Nodejs send partial response

查看:219
本文介绍了Nodejs发送部分响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mongodb集合中有20,000条记录。我在csv中导出所有这些记录。我使用这个发送部分响应:

There are 20,000 records in mongodb collection. I am exporting all these records in a csv. I am send partial response using this :

res.writeHead(200, {
                     "Content-Type": "application/csv",
                     "Content-disposition": "attachment; filename='import.csv'"
 });

res.write(data + '0', "binary");

上述代码正在批次500中执行。当所有记录都处理完毕时,

Above code is executing in batch of 500. I am ending using this code when all records are processed.

if (++responseCount == loopCount) {
  res.end();
}

但我遇到这个错误:


发送后不能设置标头。

Can't set headers after they are sent.

这是我的完整代码。

var exportData = function (req, res, next) {

var limit = 500;
var responseCount = 0;
var loopCount = 1;
var size = 30000;

//Get 500 records at one time
var getData = function (req, start, cb) {
    req.db.collection('items').find().skip(start).limit(limit).toArray(function (err, records) {
        if (err) throw err;
        cb(null, records);
    });
};

if (size > limit) {
    loopCount = parseInt(req.size / limit);

    if ((req.size % limit) != 0) {
        loopCount += 1;
    }
}

for (var j = 0; j < loopCount; j++) {

    getData(req, limit * j, function (err, records) {

        if (err) throw err;

        records.forEach(function (record) {
            //Process record one by one
        });

        res.write(records);

        if (++responseCount == loopCount) {
            res.setHeader('Content-type', 'application/csv');
            res.setHeader("Content-disposition", 'attachment; filename="import.csv"');
            res.end();

        }

    });
}
};


推荐答案

为什么不只是流式传输数据?您可以使用mongoose query.stream 功能。来自文档的示例:

Why not just stream the data? You could use mongoose query.stream feature. An example from the docs:

// follows the nodejs 0.8 stream api
Thing.find({ name: /^hello/ }).stream().pipe(res)

流将处理数据流。作为node.js流,您还可以监听事件:

The stream will take care of the data flow for you. As a node.js stream, you can also listen for events:

// manual streaming
var stream = Thing.find({ name: /^hello/ }).stream();

stream.on('data', function (doc) {
  // do something with the mongoose document
}).on('error', function (err) {
  // handle the error
}).on('close', function () {
  // the stream is closed
});

这篇关于Nodejs发送部分响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆