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.
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.
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.
I tried with 3 libraries so far:
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);
}
});
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.
Any advise?
Thanks in advace
First, you require in the adm-zip module. Next, you define the readZipArchive() function, which is an asynchronous function. Within the function, you create an instance of adm-zip with the path of the ZIP file you want to read.
You can compress 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 gzip it for you automatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With