Last active
May 2, 2022 03:50
-
-
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.
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
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NPM package for this gist: https://npmjs.org/package/ng-docserver