-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.js
More file actions
executable file
·96 lines (87 loc) · 2.64 KB
/
Copy pathrouter.js
File metadata and controls
executable file
·96 lines (87 loc) · 2.64 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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var url = require('url');
var fs = require('fs');
var staticTypes = {
"css": "text/css",
"gif": "image/gif",
"html": "text/html",
"ico": "image/x-icon",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"js": "text/javascript",
"json": "application/json",
"pdf": "application/pdf",
"png": "image/png",
"svg": "image/svg+xml",
"swf": "application/x-shockwave-flash",
"tiff": "image/tiff",
"txt": "text/plain",
"wav": "audio/x-wav",
"wma": "audio/x-ms-wma",
"wmv": "video/x-ms-wmv",
"xml": "text/xml"
};
var staticMap = [
[/^\/static\/(.*)\.(\w+)$/,'./static/$1.$2']
];
var routeMap = [
[/^\/(.*)$/,'$1']
];
var getStaticPath = function(pathname) {
var staticPath = null;
for (var i= 0,len=staticMap.length; i<len; i++) {
var reg = staticMap[i][0];
var rep = staticMap[i][1];
if (pathname.search(reg)>=0) {
staticPath = pathname.replace(reg,rep);
break;
}
}
return staticPath;
};
var route = function(request,response) {
var location = url.parse(request.url);
console.log(location.pathname);
var staticPath = getStaticPath(location.pathname);
if (staticPath!=null) {
var ext = staticPath.substring(staticPath.lastIndexOf('.')+1);
fs.readFile(staticPath,'binary',function(err,file) {
if (err) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("404 Not Found");
response.end();
} else {
response.writeHead(200, {"Content-Type": staticTypes[ext]});
response.write(file,'binary');
response.end();
}
});
return;
}
var actionName = null;
for (var i= 0,len=routeMap.length; i<len; i++) {
var reg = routeMap[i][0];
var rep = routeMap[i][1];
if (location.pathname.search(reg)>=0) {
actionName = location.pathname.replace(reg,rep);
break;
}
}
if (actionName==null) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("404 Not Found");
response.end();
return;
}
var actionFile = './action/'+actionName+'.js';
try {
var action = require(actionFile);
action.doAction(request,response);
} catch(e) {
console.log(e);
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("404 Not Found");
response.end();
return;
}
};
exports.route = route;