Skip to content Skip to sidebar Skip to footer

Multipart/mixed Response Using Nodejs

I've the following scenario: to return a multipart/mixed response that will contain the following items using NodeJS, where we control both ends of the communication so we should b

Solution 1:

Solution looks like this - called per item that needs to be written to the response.

                res.writeHead(200, {
                    'Content-Type': 'multipart/x-mixed-replace; charset=UTF-8; boundary="' + SNAPSHOT_BOUNDARY + '"',
                    Connection: 'keep-alive',
                    Expires: 'Fri, 01 Jan 1990 00:00:00 GMT',
                    'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate',
                    Pragma: 'no-cache'
                });

feed.snapshots.forEach(function(item) {
                writeResponse(item);
            });

    functionwriteResponse(item) {
        var buffer = new Buffer(0);
            var readStream = getGridFs().createReadStream({root: 'items', _id: snapshotItem._id});

            readStream.on('error', function(err) {
                if (err) {
                    // handle error
                }
            });

            readStream.on('data', function(chunk) {
                buffer = Buffer.concat([buffer, chunk]);
            });

            readStream.on('end', function() {
                res.write('\n\n' + SNAPSHOT_BOUNDARY + '\n');
                res.write('Content-Disposition: filename="' + item.filename + '" \n');
                res.write('Content-Type: application/zip \n');
                res.write('Content-length: ' + buffer.length + '\n\n');
                res.write(buffer);
            });
    }

Still having issues with supertest parsing multipart responses - ticket open at https://github.com/felixge/node-formidable/issues/348

Post a Comment for "Multipart/mixed Response Using Nodejs"