Node.js script to make a zip archive

While working in a node.js project, I had an use case where user will have to query based on days and query result will be pdf filtered by  the date range. And I have to create a directory of pdf files and return it as a zip file for the user.

As there are many modules related to zip, I tried couple of the active modules but none was meeting my requirements. And some of those modules has different bugs/issues which were open at that time. After different try/error and going through most of the zip modules, I used archiver.  And below a sample how it worked.

node_modules/*
*.data

view raw
.gitignore
hosted with ❤ by GitHub

npm install archiver;

view raw
install.sh
hosted with ❤ by GitHub

var archiver = require('archiver'),
archive = archiver('zip'),
fs = require('fs');
var output = fs.createWriteStream(__dirname + '/mocks.zip');
archive.pipe(output);
var getStream = function(fileName){
return fs.readFileSync(fileName);
}
//these are the files, want to put into zip archive
var fileNames = ['mock1.data', 'mock2.data', 'mock3.data'];
for(i=0; i<fileNames.length; i++){
var path = __dirname + '/'+fileNames[i];
archive.append(getStream(path), { name: fileNames[i]});
}
archive.finalize(function(err, bytes) {
if (err) {
throw err;
}
console.log(bytes + ' total bytes');
});

view raw
zipArchiver.js
hosted with ❤ by GitHub

 

After cloning the gist make sure you have files to zip and run the script like below:

node_archiver

Happy coding 🙂

Serving static files using Restify

I was working with Node.js for building a REST API. For REST API module I was using restify. The restify is a simple and yet powerful node module. One of the use case of the API was, I had to serve static file for specific routing. I went through the docs and tried different things but couldn’t figure yet out at first. After hustling for hours,  me and Christian started to go deep into it and figured it out!

So in my case the configuration was  something like this


server.get(/.*/, restify.serveStatic({
    'directory': 'static_content',
    'default': 'index.html'
 }));

When restify internally resolve the path, it looks for ‘static_content/index.html’.

I have coded a very basic application to show it works, sample application looks like below:

node_modules/*

view raw
.gitignore
hosted with ❤ by GitHub

<html>
<head>
</head>
<body>
Serving html from restify!
</body>
</html>

view raw
index.html
hosted with ❤ by GitHub

{
"name": "7271849",
"version": "0.0.1",
"description": "A sample application that shows how to serve static file using restify.",
"main": "api.js",
"dependencies": {
"restify": "~2.6.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"repository": {
"type": "git",
"url": "https://gist.github.com/7271849.git"
},
"keywords": [
"restify",
"node.js",
"static"
],
"author": "Mushfiq-E Mahabub",
"license": "wtfpl",
"gitHead": "2c8e160f865cc11b2b09a5681b220c442a47ab17",
"bugs": {
"url": "https://gist.github.com/7271849"
}
}

view raw
package.json
hosted with ❤ by GitHub

var restify = require('restify');
var server = restify.createServer();
server.get(/.*/, restify.serveStatic({
'directory': '.',
'default': 'index.html'
}));
server.listen(8080, function() {
console.log('%s listening at %s', server.name, server.url);
});

view raw
server.js
hosted with ❤ by GitHub

In this sample application the static content (index.html) is in the same root as server.js as why ‘directory’: ‘.’.

Use package.json to install necessary module and start playing 🙂