Created
June 23, 2013 10:50
-
-
Save pksorensen/5844595 to your computer and use it in GitHub Desktop.
How to return a zip file with express, nodejs on linux.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://stackoverflow.com/questions/5754153/zip-archives-in-node-js | |
var spawn = require('child_process').spawn; | |
app.get('/scripts/archive', function(req, res) { | |
// Options -r recursive -j ignore directory info - redirect to stdout | |
var zip = spawn('zip', ['-rj', '-', SCRIPTS_PATH]); | |
res.contentType('zip'); | |
// Keep writing stdout to res | |
zip.stdout.on('data', function (data) { | |
res.write(data); | |
}); | |
zip.stderr.on('data', function (data) { | |
// Uncomment to see the files being added | |
//console.log('zip stderr: ' + data); | |
}); | |
// End the response on zip exit | |
zip.on('exit', function (code) { | |
if(code !== 0) { | |
res.statusCode = 500; | |
console.log('zip process exited with code ' + code); | |
res.end(); | |
} else { | |
res.end(); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment