-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
55 lines (44 loc) · 1.66 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
var http = require('http');
var fs = require('fs');
var url = require('url');
var mime = require('mime');
var queryData = require("./data_fs");
http.createServer(function(request, response) {
var fileType;
var pathname = url.parse(request.url).pathname;
if(pathname == "/") {
pathname = "/index.html";
}
// 只允许获取 js 文件中 “status.js”
if(pathname.lastIndexOf(".js") !== -1 && pathname.indexOf("status.js") === -1) {
response.writeHead(404, {'content-Type': 'text/html'});
response.write("<h1 style='text-align: center;'>404 Not Found <br /> ㄟ( ▔ _ ▔ )ㄏ</h1>");
response.end();
return ;
}
console.log("Request for " + pathname + " received.");
// 输出数据
fs.readFile(pathname.substr(1), function(err, data) {
if(err) {
console.log(err);
response.writeHead(404, {'content-Type': 'text/html'});
response.write("<h1 style='text-align: center;'>404 Not Found <br /> ┑( ̄Д  ̄)┍</h1>");
} else {
fileType = mime.lookup(pathname);
console.log(fileType);
response.writeHead(200, {'content-Type': fileType });
// 获取 API 数据
if(fileType.lastIndexOf("html") !== -1) {
queryData.getStatus(queryData.options);
}
if(fileType.indexOf("image") !== -1) {
console.log(data.length);
response.write(data);
} else {
response.write(data.toString());
}
}
response.end();
});
}).listen(8080);
console.log("server running at localhost:8080");