forked from shy2850/node-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-server.js
More file actions
121 lines (116 loc) · 5.92 KB
/
node-server.js
File metadata and controls
121 lines (116 loc) · 5.92 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
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
"use strict";
var CONF = require("./nodeLib/config/conf"), //综合配置
handle = require("./nodeLib/common/handle"),//文本文件的模板操作
middleware = require("./nodeLib/filter/middleware"),//支持中间件
modules = require("./nodeLib/common/modules"),//支持的插件配置
filter = require('./nodeLib/filter/filter'),//支持前置过滤器
querystring = require("querystring"),
mime = require("mime"), //MIME类型
http = require("http"),
url = require("url"),
fs = require("fs");
var staticConf = CONF.staticconf, //静态文件服务器配置
mini = middleware.mini,
serverInfo = {needUpdate: false}; //服务器相关的一些参数。
function start(conf){
var server = http.createServer(function (req, resp) { try{
var host = (req.headers.host + ':80').split(':'),
pathurl,
hostConf = CONF[ host[0] ],
root,
agent;
if( hostConf && (host[1] | 0) === (hostConf.port | 0) ){ //域名识别
conf = hostConf;
}else if( (host[1] | 0) === 80 ){
conf = CONF.localhost;
}
root = (conf.root || __dirname); conf.root = root;
filter.execute(req,resp,conf); //过滤器提前, 可以修改url
try{pathurl = decodeURI(url.parse(req.url).pathname); }catch(e){ pathurl = req.url; }
var pathname = (pathurl === '/') ? (root + conf.welcome) : root + pathurl; //根目录时,追加welcome页面
//包装request功能
req.data = querystring.parse( url.parse(req.url).query );
req.util = {mime: mime, conf: conf, host: host[0], staticServer: "http://" + host[0] + ":" + staticConf.port + "/"};
req.$ = {title: pathurl, fileList: [], needUpdate: serverInfo.needUpdate };
var DEBUG = req.data.debug === "true" || conf.debug; //DEBUG模式判断
setTimeout( function(){
if( req.data["modify.check"] === "true" ){ // modify.check=true 时: 检测文件更新
filter.check(pathname, req.data.mtime, req, resp);
return;
}
fs.stat(pathname,function(error, stats){
if(stats && stats.isFile && stats.isFile()){ //如果url对应的资源文件存在,根据后缀名写入MIME类型
if( req.method === "POST" ){ // POST请求 添加target参数以后, 使用 upload 插件进行解析。
req.data.target = pathurl;
modules.get("upload").execute(req,resp,root,handle,conf);
return;
}
var expires = new Date();
expires.setTime( expires.getTime() + (conf.expires || 0) );
resp.writeHead(200, {
"Content-Type": mime.get(pathname) || 'text/html',
"Expires": expires
});
var rs = fs.createReadStream(pathname), s = '', ware;
rs.on('error',function(err){
throw err;
}).on('data',function(d){
s += d;
});
if( conf.middleware && (req.data.middleware !== "false") && (ware = middleware.get(pathname)) ){ //中间件处理, MIME需要mime.type中修改
rs.on('end',function(){
ware(req,resp,s,pathname,DEBUG);
});
}else if( conf.handle && mime.isTXT(pathname) && !( /[\.\-]min\.(js|css)$/.test(pathurl) ) && req.data.handle !== "false" ){ //handle
rs.on('end',function(){
handle.execute(req,resp,root,s, mini.get(pathname) ,DEBUG, conf);
});
}else{
rs.pipe(resp);
}
} else if(conf.fs_mod && stats && stats.isDirectory && stats.isDirectory()){ //如果当前url被成功映射到服务器的文件夹,创建一段列表字符串写出
require('./nodeLib/filter/directory').execute(req,resp,root,pathname,pathurl,conf,DEBUG);
} else{
var m = modules.get( pathurl.replace('/','') );
if(m){
m.execute(req,resp,root,handle,conf);
}else if(conf.agent && conf.agent.get && (agent = conf.agent.get(pathurl) ) ){ // 代理过滤
require('./nodeLib/filter/agent').execute(req,resp,agent,req.url);
return;
}else{
resp.writeHead(404, {"Content-Type": "text/html"});
fs.readFile(conf.notFound || '', function (err, data){
if(err){
if( conf.notFound ){ console.log(err); }
resp.end( '<h1 style="font-size:200px;text-align:center;">404</h1>' );
}else{
resp.end( data );
}
});
}
}
});
}, req.data.delay | 0 );// 增加delay参数,使得所有GET请求可以动态延时
}catch(err){
console.log(err.stack);
resp.writeHead(500, {"Content-Type": "text/html"});
resp.end( err.stack.toString().replace(/\n/g,"<br>") );
}});
server.maxConnections = conf.maxConnections;
server.listen(conf.port);
return server;
}
var ports = {}, extCmd = ([]).slice.call( process.argv, 2 ).join(' ');
for(var k in CONF){
(function(c){
if(ports[c.port]){return;}
ports[c.port] = start(c);
})(CONF[k]);
console.log("Server running at http://127.0.0.1:" + CONF[k].port + '\t[' + k + ']');
}
if(extCmd){ require('child_process').exec( extCmd ); }
try{
require('./nodeLib/config/update').execute(serverInfo);
}catch(e){
console.log( e );
}