-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
124 lines (117 loc) · 4.11 KB
/
server.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
var http = require('http');
var url = require('url');
var fs = require('fs');
var path = require('path');
//配置
var config = {
port: 1234,
denyAccess: ['./httpserver.js'],
localIPs: ['10.113.1.174'],
srcpath: '/pages'
};
//开始HTTP服务器
http.createServer(processRequestRoute).listen(config.port);
console.log("Server has started. port:"+config.port);
//路由URL
function processRequestRoute(request, response) {
var pathname = url.parse(request.url).pathname;
if (pathname === '/') {
pathname = "/index.html"; //默认页面
}
var ext = path.extname(pathname);
var localPath = ''; //本地相对路径
var staticRes = false; //是否是静态资源
if (ext.length > 0) {
localPath = '.' + pathname;
staticRes = true;
} else {
localPath = '.' + config.srcpath + pathname + '.js';
staticRes = false;
}
//禁止远程访问
if (config.denyAccess && config.denyAccess.length > 0) {
var islocal = false;
var remoteAddress = request.connection.remoteAddress;
for (var j = 0; j < config.localIPs.length; j++) {
if (remoteAddress === config.localIPs[j]) {
islocal = true;
break;
}
}
if (!islocal) {
for (var i = 0; i < config.denyAccess.length; i++) {
if (localPath === config.denyAccess[i]) {
response.writeHead(403, { 'Content-Type': 'text/plain' });
response.end('403:Deny access to this page');
return;
} else {
// staticResHandler(localPath, ext, response);
}
}
}
}
//禁止访问后端js
// if (staticRes && localPath.indexOf(config.srcpath) >= 0) {
// response.writeHead(403, { 'Content-Type': 'text/plain' });
// response.end('403:Deny access to this page');
// return;
// }
fs.exists(localPath, function (exists) {
if (exists) {
if (staticRes) {
staticResHandler(localPath, ext, response); //静态资源
} else {
try {
var handler = require(localPath);
if (handler.processRequest && typeof handler.processRequest === 'function') {
handler.processRequest(request, response); //动态资源
} else {
response.writeHead(404, { 'Content-Type': 'text/plain' });
response.end('404:Handle Not found');
}
} catch (exception) {
console.log('error::url:' + request.url + 'msg:' + exception);
response.writeHead(500, { "Content-Type": "text/plain" });
response.end("Server Error:" + exception);
}
}
} else { //资源不存在
response.writeHead(404, { 'Content-Type': 'text/plain' });
response.end('404:File Not found');
}
});
}
//处理静态资源
function staticResHandler(localPath, ext, response) {
fs.readFile(localPath, "binary", function (error, file) {
if (error) {
response.writeHead(500, { "Content-Type": "text/plain" });
response.end("Server Error:" + error);
} else {
response.writeHead(200, { "Content-Type": getContentTypeByExt(ext) });
response.end(file, "binary");
}
});
}
//得到ContentType
function getContentTypeByExt(ext) {
ext = ext.toLowerCase();
if (ext === '.htm' || ext === '.html')
return 'text/html';
else if (ext === '.js')
return 'application/x-javascript';
else if (ext === '.css')
return 'text/css';
else if (ext === '.jpe' || ext === '.jpeg' || ext === '.jpg')
return 'image/jpeg';
else if (ext === '.png')
return 'image/png';
else if (ext === '.ico')
return 'image/x-icon';
else if (ext === '.zip')
return 'application/zip';
else if (ext === '.doc')
return 'application/msword';
else
return 'text/plain';
}