-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.js
82 lines (77 loc) · 2.16 KB
/
serve.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
import http from 'http';
import fs from 'fs';
import url from 'url';
import path from "path";
import c from 'child_process';
// 创建服务器
http.createServer( function (request, response) {
// 解析请求,包括文件名
var pathname = url.parse(request.url).pathname;
var extname = path.extname(pathname);
// 输出请求的文件名
// console.log("Request for " + pathname + " received.");
// 从文件系统中读取请求的文件内容
fs.readFile(pathname.substr(1), function (err, data) {
let type = memu(extname);
if (err) {
response.writeHead(404, {'Content-Type': type});
}else{
response.writeHead(200, {
'Content-Length': data.length,
'Content-Type': type
});
// 响应文件内容
response.write(data);
}
// 发送响应数据
response.end();
});
}).listen(9000);
c.exec('start http://localhost:9000/index.html');
function memu(exname){
switch(exname){
case ".html":
return "text/html";
break;
case ".css":
return "text/css";
break;
case ".jpg":
return "image/jpg";
break;
case ".png":
return "image/png";
break;
case ".js":
return "text/javascript";
break;
case ".json":
return "text/json";
break;
case ".jpeg":
return "image/jpeg";
break;
case ".gif":
return "image/gif";
break;
case ".svg":
return "image/svg+xml";
break;
case ".rtf":
return "application/rtf";
break;
case ".woff":case ".woff2":
return "application/x-font-woff";
break;
case ".webm":
return "video/webm";
break;
default:
return "text/plain";
}
}
// 控制台会输出以下信息
console.log(' \n \
请勿关闭本窗口,打开浏览器访问下列地址 \n\
');
console.log(' http://localhost:9000/')