-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
152 lines (133 loc) · 4.47 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
147
148
149
150
151
152
var http = require('http');
var url = require('url');
// first
// http.createServer(function(request, response) {
// response.writeHead(200, {'Content-Type':'text/plain'});
// response.write('Hello World!');
// response.end();
// }).listen(8888);
// second
// function onRequest(request, response){
// response.writeHead(200, {'Content-Type':'text/plain'});
// response.write('Hello World 2!');
// response.end();
// };
// http.createServer(onRequest).listen(8888);
// third
// function onRequest(request, response) {
// console.log('Request is received!')
// response.writeHead(200, {'COntent-Type':'text/plain'});
// response.write('Hello World three!');
// response.end();
// };
// http.createServer(onRequest).listen(8888);
// console.log('Server has started!');
// four
// function start(){
// function onRequest(request, response){
// console.log('Request is received!');
// response.writeHead(200, {'Content-Type':'text/plain'});
// response.write('Hello World four!');
// response.end();
// };
// http.createServer(onRequest).listen(8888);
// console.log('Server has started!');
// };
// exports.start = start;
// five
// function start(){
// function onRequest(request, response){
// var pathname = url.parse(request.url).pathname;
// console.log('Request for ' + pathname + ' received!');
// response.writeHead(200, {'COntent-Type':'text/plain'});
// response.write('Hello World five!');
// response.end();
// };
// http.createServer(onRequest).listen(8888);
// console.log('Server has started!');
// };
// exports.start = start;
// 增加路由
// function start(route){
// function onRequest(request, response){
// var pathname = url.parse(request.url).pathname;
// console.log('Request for ' + pathname + 'received!');
// route(pathname);
// response.writeHead(200, {'Content-Type':'text/plain'});
// response.write('Hello World six!');
// response.end();
// }
// http.createServer(onRequest).listen(8888);
// console.log('Server has started!');
// }
// exports.start = start;
// 增加路由处理模块
// function start(route, handle){
// function onRequest(request, response){
// var pathname = url.parse(request.url).pathname;
// console.log('Request for ' + pathname + ' received!');
// route(handle, pathname);
// response.writeHead(200, {'Content-Type':'text/plain'});
// response.write('Hello World seven!');
// response.end();
// }
// http.createServer(onRequest).listen(8888);
// console.log('Server has started!');
// }
// exports.start = start;
// 返回路由处理请求结果到浏览器
// function start(route, handle){
// function onRequest(request, response){
// var pathname = url.parse(request.url).pathname;
// console.log('Request for ' + pathname + ' received!');
// response.writeHead(200, {'COntent-Type':'text/plain'});
// var content = route(handle, pathname);
// response.write(content);
// response.end();
// }
// http.createServer(onRequest).listen(8888);
// console.log('Server has started!');
// }
// exports.start = start;
//把response对象作为路由处理函数的参数
// function start(route, handle){
// function onRequest(request, response){
// var pathname = url.parse(request.url).pathname;
// console.log('Request for ' + pathname + ' received!');
// route(handle, pathname, response);
// }
// http.createServer(onRequest).listen(8888);
// console.log('Server has started!');
// }
// exports.start = start;
//给request对象增加data和end监听事件
// function start(route, handle){
// function onRequest(request, response){
// var postData = '';
// var pathname = url.parse(request.url).pathname;
// console.log('Request for ' + pathname + ' received!');
// request.setEncoding('utf8');
// request.addListener('data', function(postDataChunk){
// postData += postDataChunk;
// console.log('Request POST data chunk ' + postDataChunk + ' !');
// });
// request.addListener('end', function(){
// route(handle, pathname, response, postData);
// });
// }
// http.createServer(onRequest).listen(8888);
// console.log('Server has started!');
// }
// exports.start = start;
var http = require("http");
var url = require("url");
function start(route, handle) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
route(handle, pathname, response, request);
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;