anyproxy/rule_sample/rule_use_local_data.js
2014-10-21 13:44:22 +08:00

27 lines
657 B
JavaScript

//replace all the images with local one
var url = require("url"),
path = require("path"),
fs = require("fs");
var LOCAL_IMAGE = "/Users/path/to/image.png";
module.exports = {
//mark if use local response
shouldUseLocalResponse : function(req,reqBody){
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(LOCAL_IMAGE) );
}
}
};