node.js中的JSON Zip响应 [英] JSON Zip Response in node.js

查看:102
本文介绍了node.js中的JSON Zip响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对node.js来说非常新鲜,我试图发回一个包含JSON结果的zip文件。
我一直在试图弄清楚如何做,但没有预期的结果。

I'm pretty new to node.js and I'm trying to send back a zip file containing JSON results. I've been trying to figure it out how to do it, but haven't had the expected results.

我正在使用NodeJS,ExpressJS,机车JS,Mongoose和MongoDB。

I'm using NodeJS, ExpressJS, LocomotiveJS, Mongoose and MongoDB.

由于我们正在构建面向移动的应用程序,我尽可能保存尽可能多的带宽。

Since we're building a mobile oriented application, I'm trying to save as many as bandwith as I can.

移动应用程序的日常初始加载可能是一个大的JSON文档,因此我希望将其压缩后才能发送到设备。如果可能,我想在内存中做所有的事情,以避免磁盘I / O。

The daily initial load for the mobile app could be a big JSON document, so I want to zip it before sending it to the device. If possible I'd like to do it everything in memory in order to avoid disk I/O.

我尝试了3个库到目前为止:

I tried with 3 libraries so far:


  • adm-zip

  • node-zip

  • zipstream

我获得的最好的结果是使用node-zip。这是我的代码:

The best result I achieved is using node-zip. Here's my code:

  return Queue.find({'owners': this.param('id')}).select('name extra_info cycle qtype purge purge_time tasks').exec(function (err, docs) {
    if (!err) {
      zip.file('queue.json', docs);
      var data = zip.generate({base64:false,compression:'DEFLATE'});

      res.set('Content-Type', 'application/zip');
      return res.send(data);
    }
    else {
      console.log(err);
      return res.send(err);
    }
  });

结果是下载的zip文件,但内容无法读取。

The result is a downloaded zip file but the content is unreadable.

我很确定我正在混合的东西,但到目前为止,我不知道如何继续。

I'm pretty sure I'm mixing up things, but to this point I'm not sure how to proceed.

任何建议?

感谢advace

推荐答案

这样:

app.configure(function(){
  //....
  app.use(express.compress());
});


app.get('/foo', function(req, res, next){
  res.send(json_data);
});

如果用户代理支持gzip,它将自动为您提供gzip。

If the user agent supports gzip it will gzip it for you automatically.

这篇关于node.js中的JSON Zip响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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