update local rule

This commit is contained in:
加里 2014-10-21 13:44:22 +08:00
parent c6c2355d17
commit 3104ee167c

View File

@ -1,53 +1,25 @@
//replace all the images with local one
var url = require("url"),
path = require("path"),
fs = require("fs"),
buffer = require("buffer");
fs = require("fs");
var map = [
{
"host" :/./,
"path" :/\.(png|gif|jpg|jpeg)/,
"localFile" :"/Users/Stella/tmp/test.png",
"localDir" :"~/"
}
];
var LOCAL_IMAGE = "/Users/path/to/image.png";
module.exports = {
//mark if use local response
shouldUseLocalResponse : function(req,reqBody){
var host = req.headers.host,
urlPattern = url.parse(req.url),
path = urlPattern.path;
for(var index in map){
var rule = map[index];
var hostTest = new RegExp(rule.host).test(host),
pathTest = new RegExp(rule.path).test(path);
if(hostTest && pathTest && (rule.localFile || rule.localDir) ){
var targetLocalfile = rule.localFile;
//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);
}
if(fs.existsSync(targetLocalfile)){
console.log("==>local file: " + targetLocalfile);
req.replaceLocalFile = targetLocalfile; //add a flag to req object
if(/\.(png|gif|jpg|jpeg)$/.test(req.url)){
req.replaceLocalFile = true;
return true;
}
}
}
}else{
return false;
}
},
dealLocalResponse : function(req,reqBody,callback){
if(req.replaceLocalFile){
callback(200, {"content-type":"image/png"}, fs.readFileSync(req.replaceLocalFile) );
callback(200, {"content-type":"image/png"}, fs.readFileSync(LOCAL_IMAGE) );
}
}
};