forked from shy2850/node-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle.js
More file actions
35 lines (35 loc) · 1.07 KB
/
Copy pathhandle.js
File metadata and controls
35 lines (35 loc) · 1.07 KB
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
"use strict";
var fs = require("fs"),
_ = require("underscore");
exports.execute = function(req, resp, root, str, mini, debug, conf){
var belong = "$[placeholder]";
var h = /\$belong\[["\s]*([^"\s]+)["\s]*\]/.exec(str);
try{
if(h){
belong = fs.readFileSync( root + "/" + h[1],'utf-8'); //读取belong文本
str = str.replace(h[0], "" ); //替换关键字
str = belong.replace("$[placeholder]",str);
}
str = str.replace(/\$include\[["\s]*([^"\s]+)["\s]*\]/g, function(match, key){
return fs.readFileSync( root + "/" + key,'utf-8'); //读取include文本
});
var result = str;
if(conf.runJs){ //完全使用underscore内置template引擎
var compiled = _.template(str);
result = compiled({request: req, response: resp, require: require});
}
switch(typeof result){
case "function": result(); return;
case "string":
default :
if( debug ) {
resp.end( result );
}else{
mini(result,resp);
}
}
}catch(e){
resp.writeHead(500, {"Content-Type": "text/html"});
resp.end( e.stack.toString().replace(/\n/g,"<br>") );
}
};