JSON Zip Response in node.js

I am new to node.js and I am trying to send back a zip file containing JSON results. I tried to figure out how to do this, but did not expect results.

I use NodeJS, ExpressJS, LocomotiveJS, Mongoose and MongoDB.

Since we are creating a mobile oriented application, I am trying to save as much as possible on tape.

A daily initial download for a mobile application can be a large JSON document, so I want to pin it before sending it to the device. If possible, I would like to do everything in memory to avoid disk I / O.

I tried with 3 libraries:

  • admin lightning
  • node-zip
  • zipstream

The best result I have achieved is to use node-zip. Here is 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); } }); 

The result is a downloaded zip file, but the contents are unreadable.

I am sure that I am mixing things, but so far I am not sure how to proceed.

Any tips?

Thanks at advace

+7
source share
3 answers

You can compress the output in express 3 with this:

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

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

+17
source

For Express 4+, compression is not supplied with Express and must be installed separately.

 $ npm install compression 

Then, to use the library:

 var compression = require('compression'); app.use(compression()); 

There are several options that you can customize, see here for a list .

+1
source

I think you mean how to send gzip content using node?

Node version 0.6 and higher have a built-in zlip module, so there is no need to require external modules.

You can send gzip content this way.

  response.writeHead(200, { 'content-encoding': 'gzip' }); json.pipe(zlib.createGzip()).pipe(response); 

Obviously, you will need to first check the weather when the client accepts Gzip encoding, and also remember that gzip is an expensive operation, so you should cache the results.

Here is a complete example from the docs

 // server example // Running a gzip operation on every request is quite expensive. // It would be much more efficient to cache the compressed buffer. var zlib = require('zlib'); var http = require('http'); var fs = require('fs'); http.createServer(function(request, response) { var raw = fs.createReadStream('index.html'); var acceptEncoding = request.headers['accept-encoding']; if (!acceptEncoding) { acceptEncoding = ''; } // Note: this is not a conformant accept-encoding parser. // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3 if (acceptEncoding.match(/\bdeflate\b/)) { response.writeHead(200, { 'content-encoding': 'deflate' }); raw.pipe(zlib.createDeflate()).pipe(response); } else if (acceptEncoding.match(/\bgzip\b/)) { response.writeHead(200, { 'content-encoding': 'gzip' }); raw.pipe(zlib.createGzip()).pipe(response); } else { response.writeHead(200, {}); raw.pipe(response); } }).listen(1337); 
0
source

All Articles