improve file mapping

This commit is contained in:
加里
2014-08-18 19:51:15 +08:00
parent a0fd07fbc8
commit e1053b15d4
3 changed files with 21 additions and 10 deletions

View File

@@ -27,7 +27,7 @@ var handleRule = {
function userRequestHandler(req,userRes){
var host = req.headers.host,
path = url.parse(req.url).path,
path = url.parse(req.url).pathname,
ifLocalruleMatched = false;
console.log(color.green("\nreceived request to : " + host + path));
@@ -47,10 +47,11 @@ function userRequestHandler(req,userRes){
console.log("==>meet the rules, will map to local file");
var targetLocalfile = rule.localFile;
if(!targetLocalfile){ //find file in dir //TODO : /a/b/c -> b/c
var basename = pathUtil.basename(path);
basename = basename.slice(0,basename.indexOf("?")); //remove chars after question mark
targetLocalfile = pathUtil.join(rule.localDir,basename);
//localfile not set, map to dir
if(!targetLocalfile){ //find file in dir, /a/b/file.html -> dir + b/file.html
var remotePathWithoutPrefix = path.replace(new RegExp(rule.path),""); //remove prefix
targetLocalfile = pathUtil.join(rule.localDir,remotePathWithoutPrefix);
}
console.log("==>local file: " + targetLocalfile);