-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (43 loc) · 1.25 KB
/
index.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var fs = require("fs");
var url = require("url");
var p = require("path");
var appRoot = require('app-root-path');
module.exports = function(req,res,next){
var path = url.parse(req.originalUrl).pathname;
if(!/^\/?bc\/.*/.test(path)) return next();
if(/.*\/\.\.?\/.*/.test(path)) return next();
if(/.*\/\/.*/.test(path)) return next();
raw(path,function(file){
if(file) return res.sendFile(file);
next();
});
};
module.exports.changeAppRoot = function (rootPath) {
appRoot = rootPath;
};
function raw(path,next){
path = path.split("/").slice(2);
var wp = path;
path = appRoot+"/bower_components/"+path[0]+"/";
fs.readFile(path+"bower.json",function(err,file){
if(err) {
console.log('error reading ', path,'/bower.json');
return next();
}
file = JSON.parse(file);
if(!("main" in file)) return next();
if(!Array.isArray(file.main))
return next(p.resolve(path,file.main));
if(file.main.length == 1 && wp.length == 1)
return next(p.resolve(path,file.main[0]));
wp.shift();
var r = p.resolve(path,wp.join("/"));
while(file.main.length > 0){
var f = p.resolve(path,file.main.pop());
if(f == r)
return next(r);
}
return next();
});
}
module.exports.raw = raw;