-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (61 loc) · 1.66 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
52
53
54
55
56
57
58
59
60
61
62
63
64
var express=require("express");
var bodyParser = require('body-parser');
var fs=require("fs")
var jade=require("jade");
var route=require("./route")
var app_tools=require("./tools/app");//自定义工具
var app=express();
app.use('/public',express.static(__dirname+"/public"));//设置静态文件目录
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
/*默认路由*/
app.all("*",function(req,res,next){
console.log(req.path);
next();
});
/*先过滤具体路由*/
app.all("*",function(req,res,next){
var path=req.path;
var isRoute=false;
for(var i in route){
var reg=new RegExp("^"+i+"$");
if(reg.test(path)){
isRoute=true;
route[i](req,res);
}
}
if(!isRoute){
next();
}
});
/*没有路由,根据规则匹配具体的action,没有action匹配文件,没有文件返回404*/
app.all("*",function(req,res){
var path=req.path;
var isRoute=false;
var result=/^((\/[^\/]+?){2})(\.html|\.htm)*$/.exec(path);
if(result==null){
app_tools.response_file(path,req,res);
}else{
var action_path=result[1].slice(1);
var action_arr=action_path.split("/");
var require_path="";
for(var i=0,len=action_arr.length;i<len-1;i++){
require_path+=action_arr[i]+"/";
}
require_path=require_path.slice(0,-1);
if(require_path){
fs.exists("./action/"+require_path+".js",function(exists){
if(exists){
var action=require("./action/"+require_path);
if(typeof action[action_arr[action_arr.length-1]]=="undefined"){
app_tools.response_file(path,req,res);
return;
}
action[action_arr[action_arr.length-1]](req,res);
}else{
app_tools.response_file(path,req,res);
}
});
}
}
});
app.listen(8081);