forked from fengfancky/WebEPG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_server.js
49 lines (36 loc) · 1.33 KB
/
web_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
var http = require('http');
var fs = require('fs');
var url = require('url');
var ContentType = require('./server/content_type')
var contentType = new ContentType();
// 创建服务器
http.createServer( function (request, response) {
// 解析请求,包括文件名
var pathname = url.parse(request.url).pathname;
if(pathname.substr(1) == 'msg'){
response.writeHead(200, {'Content-Type': 'text/plain;charset=UTF-8'});
response.write('《流浪地球》火爆上线!');
response.end();
}else{
var type = contentType.getType(pathname);
fs.readFile(pathname.substr(1), function (err, data) {
if (err) {
console.log(err);
response.writeHead(404, {'Content-Type': 'text/html;charset=UTF-8'});
}else{
// HTTP 状态码: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': type});
if(pathname.endsWith('.png')||pathname.endsWith('.jpg')){
response.write(data,'binary');
}else{
response.write(data);
}
}
// 发送响应数据
response.end();
});
}
}).listen(8080);
// 控制台会输出以下信息
console.log('Server running at http://127.0.0.1:8080/');