-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
146 lines (132 loc) · 3.67 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var PORT = 3000;
var http = require('http');//提供web服务
var url = require('url');//解析get请求
var query = require("querystring");//解析post请求
var fs = require('fs');
var mime = require('./mime').types;
var path = require('path');
var scripts;
//server1
var server1 = http.createServer(function(request, response){
//判断是GET/POST请求
if(request.method == "GET"){
var urlStr = url.parse(request.url);
var pathname = urlStr.pathname;
var realPath = path.join("webapp",pathname);
console.log(realPath)
var ext = path.extname(realPath);
ext = ext ? ext.slice(1) : 'unknown';
fs.exists(realPath, function(exists){
if(!exists){
response.writeHead(404,{
'Content-Type' : 'text/plain'
});
response.write("This request URL" + pathname + "was not found on this server.");
response.end();
}else{
if(ext == 'unknown'){
console.log(scripts)
response.write(scripts);
response.end();
return;
}
fs.readFile(realPath, "binary", function(err, file){
if(err){
response.writeHead(500, {
'Content-Type' : 'text/plain'
});
response.end(err);
}else{
var contentType = mime[ext] || "text/plain";
console.log(contentType)
response.writeHead(200, {
'Content-Type' : contentType
});
response.write(file, "binary");
response.end();
}
});
}
});
}else{
var postdata = "";
request.addListener("data",function(postchunk){
postdata += postchunk;
})
//POST结束输出结果
request.addListener("end",function(){
var params = query.parse(postdata);
console.log(params);
scripts = params.messages;
response.writeHead(301,{
'Location':'http://localhost:3000/hackedsite/list.html'
});
response.end();
})
}
});
server1.listen(3000);
console.log("Server runing at port: " + 3000 + ".");
//server2
var cookie;
var server2 = http.createServer(function(request, response){
//判断是GET/POST请求
if(request.method == "GET"){
var urlStr = url.parse(request.url);
var pathname = urlStr.pathname;
cookie = urlStr.query;
var realPath = path.join("webapp",pathname);
console.log(realPath)
var ext = path.extname(realPath);
ext = ext ? ext.slice(1) : 'unknown';
fs.exists(realPath, function(exists){
if(!exists){
response.writeHead(404,{
'Content-Type' : 'text/plain'
});
response.write("This request URL" + pathname + "was not found on this server.");
response.end();
}else{
if(ext == 'unknown'){
var data = {a:1,b:2}
response.write(JSON.stringify(data));
response.end();
return;
}
fs.readFile(realPath, "binary", function(err, file){
if(err){
response.writeHead(500, {
'Content-Type' : 'text/plain'
});
response.end(err);
}else{
var contentType = mime[ext] || "text/plain";
console.log(contentType)
response.writeHead(200, {
'Content-Type' : contentType
});
response.write(file, "binary");
response.end();
}
});
}
});
}else{
var postdata = "";
request.addListener("data",function(postchunk){
postdata += postchunk;
})
//POST结束输出结果
request.addListener("end",function(){
response.writeHead(200, {
'Content-Type' : 'text/plain'
});
var params = query.parse(postdata);
//scripts = params.messages;
response.write('1');
response.end();
})
}
});
server2.listen(3100);
console.log("Server runing at port: " + 3100 + ".");