使用NodeJS进行多部分/混合响应 [英] multipart/mixed response using NodeJS

查看:152
本文介绍了使用NodeJS进行多部分/混合响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:使用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.

  1. JSON文件,其中包含描述每个ZIP的节点列表,即 [{{名称:test1,desc:Test1 Desc,md5:1234ABCD,文件:zip-01.zip},{{名称:test1,desc:Test1 Desc,md5:1234ABCD,文件:zip-02.zip}]
  2. 从mongo gridfs商店读取ZIP文件
  1. JSON file containing a list of nodes describing each ZIP i.e. [{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

--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--

我需要将此流返回给用户,以便他们可以处理JSON文件,并在需要时扩展他们感兴趣的特定ZIP文件.

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.

通过查看API指南 http://expressjs.com/api.html 我没有看看这怎么可能?我已经正确返回了单个ZIP文件,但需要支持此业务方案.

From looking at the API guide http://expressjs.com/api.html I dont see how this is possible? I've got single ZIP files being returned correctly but need to support this business scenario.

我正在尝试创建类似于以下内容的内容: 使用Perl或PHP的HTTP多部分响应

I'm trying to create something similar to the following: HTTP multipart response using Perl or PHP

资源应包含JSON文件和所有关联的ZIP.

The res should contain a JSON file and all the associated ZIP's.

感谢您的帮助.谢谢.

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

超级测试解析多部分响应仍然存在问题-在 https://github上打开票证. com/felixge/node-formidable/issues/348

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

这篇关于使用NodeJS进行多部分/混合响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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