简体   繁体   English

使用NodeJS进行多部分/混合响应

[英]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 be able to eliminate interoperability issues. 我有以下情形:使用NodeJS返回包含以下各项的分段/混合响应,在此我们控制通信的两端,因此我们应该能够消除互操作性问题。

  1. JSON file containing a list of nodes describing each ZIP ie [{name: test1, desc: Test1 Desc, md5: 1234ABCD, file: zip-01.zip}, {name: test1, desc: Test1 Desc, md5: 1234ABCD, file: zip-02.zip}] 包含描述每个ZIP的节点列表的JSON文件,即[{name:test1,desc:Test1 Desc,md5:1234ABCD,file:zip-01.zip},{name:test1,desc:Test1 Desc,md5:1234ABCD,file :zip-02.zip}]
  2. ZIP files are read from a mongo gridfs store 从mongo gridfs商店读取ZIP文件
 --whoop Content-Disposition: attachment; name="zip"; filename="tobi.zip" Content-Type: application/zip ... data here ... --whoop Content-Disposition: form-data; name="name" Content-Type: text/plain Tobi --whoop-- 

I need to stream this back to the user so that they can process the JSON file and if required, expand out the specific ZIP file they are interested in. 我需要将此流返回给用户,以便他们可以处理JSON文件,并在需要时扩展他们感兴趣的特定ZIP文件。

From looking at the API guide http://expressjs.com/api.html I dont see how this is possible? 通过查看API指南http://expressjs.com/api.html,我看不到这是怎么可能的? I've got single ZIP files being returned correctly but need to support this business scenario. 我已经正确返回了单个ZIP文件,但需要支持此业务方案。

I'm trying to create something similar to the following: HTTP multipart response using Perl or PHP 我正在尝试创建与以下内容类似的内容: 使用Perl或PHP的HTTP多部分响应

The res should contain a JSON file and all the associated ZIP's. 资源应包含一个JSON文件和所有关联的ZIP。

Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

J Ĵ

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

    function writeResponse(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 超级测试解析多部分响应仍然存在问题-票证位于https://github.com/felixge/node-formidable/issues/348

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM