Nodes sends partial response not working

I need to send a partial response from nodejs, but this code does not work. It will immediately receive 500 records from the database and then process each record one at a time. I want to send a partial response from node.js. If I store data in an array, a buffer overflow error occurs.

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();

            }

        });
    }
};
+4
source share
1 answer

: . , , - , , - , , . express/http.Server. , , express/http.Server, .

, , result, .

var through = require('through');

function exportData(request, result) {
  var limit = 500;

  res.setHeader('Content-type', 'application/csv');
  res.setHeader('Content-disposition', 'attachment: filename="import.csv"');

  req.db.collection('items')
    .find()
    .limit(limit)
    .stream()
    .pipe(through(function(record) {
      var processedRecord = processRecord(record);
      this.write(processedRecord);
    }))
    .pipe(result);
}


function processRecord(record) {
  // process record one by one
  return record;
}
0

All Articles