Skip to content

Instantly share code, notes, and snippets.

@virasak
Last active May 2, 2022 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save virasak/5378535 to your computer and use it in GitHub Desktop.
Save virasak/5378535 to your computer and use it in GitHub Desktop.
AngularJS Offline Document Server with NodeJS. Serve AngularJS documents from the distributed zip file without extraction. Windows user may find this script useful, because the documents contain illegal file names for Windows. This script requires `mime` and `adm-zip` packages.
if (process.argv.length < 3) {
console.log('Usage: node docserver.js angular-x.y.z.zip [port]');
return;
}
var zipfile = process.argv[2],
port = Number(process.argv[3]) || 1337,
path = require('path'),
dirname = path.basename(zipfile, '.zip'),
http = require('http'),
url = require('url'),
mime = require('mime'),
docZip = require('adm-zip')(zipfile);
http.createServer(function(req, res){
var uri = url.parse(req.url).pathname;
if (['/', '/docs', '/docs/'].indexOf(uri) > -1) {
res.writeHead(302, {'Location': '/docs/index.html'});
res.end();
return;
}
docZip.readFileAsync(dirname + uri, function(data){
if (data === null) {
if (uri.indexOf('/docs/') === 0) {
res.writeHead(302, {'Location': '/docs/index.html#!' + uri.substr(5)});
res.end();
} else {
res.writeHead(404);
res.end();
}
return;
}
res.writeHead(200, {'Content-Type': mime.lookup(uri)});
res.end(data);
});
}).listen(port);
@virasak
Copy link
Author

virasak commented Jul 29, 2013

NPM package for this gist: https://npmjs.org/package/ng-docserver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment