-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpServer.js
42 lines (34 loc) · 1.08 KB
/
httpServer.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
var http = require("http"),
path = require("path"),
url = require('url') ;
var server = http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname;
console.log('uri: '+uri);
//模拟一个text/html响应
if (uri=='/'){
var queryObject = url.parse(request.url,true).query;
console.log('request GET params: '+queryObject);
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<!DOCTYPE 'html'>");
response.write("<html>");
response.write("<head>");
response.write("<title>世界你好</title>");
response.write("</head>");
response.write("<body>");
response.write("世界你好");
response.write("</body>");
response.write("</html>");
response.end();
}
//模拟一个JSON响应
if(uri=='/json'){
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ message: 'OK' }));
}
if(uri=='/upload'){
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ message: 'OK' }));
}
});
server.listen(80);
console.log("Server is listening");