forked from sihorton/appjs-appPackager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packagedApp.js
35 lines (34 loc) · 1014 Bytes
/
packagedApp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Provides ability to serve application files from package file.
* In app.js add: app.router.use(require('./packagedApp.js')(__dirname + '/example-app.appjs'));
* author: Sihorton
*/
var mime = require('mime'),
AdmZip = require('adm-zip');
module.exports = function packagedAppRouter(root) {
try {
var packagedApp = new AdmZip(root);
} catch(e) {
throw (e);
//return function router(request, response, next) { next(); };
}
return function router(request, response, next){
if (request.method === 'get') {
var url = request.pathname === '/' ? '/index.html' : request.pathname;
if (packagedApp && packagedApp.getEntry(url.substring(1))) {
var mimetype = mime.lookup(url);
packagedApp.readFileAsync(url.substring(1),function(buffer,err) {
if (err) {
response.send(500,'text/plain',new Buffer("500: Internal Server Error\n"+err, "utf-8"));
} else {
response.send(200,mimetype,buffer);
}
});
} else {
next();
}
} else {
next();
}
};
}