-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
131 lines (111 loc) · 4.08 KB
/
index.js
File metadata and controls
131 lines (111 loc) · 4.08 KB
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
const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');
const zlib = require('zlib');
const mine = require('mime-types');
const jsonConfig = readConfigData();
function staticServer() {
const defaults = {
hostname: 'localhost',
port: 3000,
root: '',
index: 'index.html',
gzip: true,
compress: 'html|css|js',
openBrowser: false
}
const settings = Object.assign({}, defaults); // 合并配置
if (jsonConfig && jsonConfig['httpOptions']){
Object.assign(settings, jsonConfig['httpOptions']);
}
const rootPath = getHtmlRoot(settings.root);// 获取静态根目录路径
const server = http.createServer((request, response)=>{
let pathname = url.parse(request.url).pathname;
//console.log("pathname" + pathname);
if (pathname.slice(-1) === '/' && settings.index && settings.index !== '/'){
response.writeHead(302, {
'Location': '/' + settings.index
})
response.end();
return;
}
const filePath = path.join(rootPath, pathname);
console.log("filePath: " + filePath);
if (fs.existsSync(filePath)){
try {
const mimeType = mine.lookup(filePath);
response.setHeader('Content-Type',mimeType || 'text/plain');
const raw = fs.createReadStream(filePath);
const ext = path.extname(filePath).slice(1);
const acceptEncoding = request.headers['accept-encoding'];
const gzipExt = settings.gzip && settings.compress.includes(ext); // 开启了gzip压缩,且文件类型在压缩格式范围内
if (gzipExt && acceptEncoding.includes('gzip')) {
response.writeHead(200, "Ok", { 'Content-Encoding': 'gzip' });
raw.pipe(zlib.createGzip()).pipe(response);
} else if (gzipExt && acceptEncoding.includes('deflate')) {
response.writeHead(200, "Ok", { 'Content-Encoding': 'deflate' });
raw.pipe(zlib.createDeflate()).pipe(response);
} else {
response.writeHead(200, "Ok");
raw.pipe(response);
}
}
catch (err){
response.writeHead(500,{'Content-Type':'text/plain'});
//console.log(err.);
response.end();
}
}
else{
response.writeHead(400,{'Content-Type':'text/plain'});
response.write("this request URL : " + pathname + " is not found!");
response.end();
}
});
// 打开默认浏览器
const openDefaultBrowser = (url) => {
const { exec } = require('child_process');
console.log(process.platform)
switch (process.platform) {
case "darwin":
exec('open ' + url);
break;
case "win32":
exec('start ' + url);
break;
default:
exec('xdg-open', [url]);
}
}
//监听主机端口
const { hostname, port, openBrowser } = settings;
server.listen(port, hostname, () => {
const url = `http://${hostname}:${port}`
console.log(`服务器运行在 ${url}`);
openBrowser && openDefaultBrowser(url); // 打开默认浏览器
});
}
function readConfigData(){
let configPath = path.join(process.cwd(), './config.json');
if (fs.existsSync(configPath)){
return JSON.parse(fs.readFileSync(configPath, 'utf8'));
}
return null;
}
function getHtmlRoot(root){
if (!root) return process.cwd();
else return root;
}
// 启动静态服务器
staticServer();
/*
staticServer({
hostname: '127.0.0.1', //主机
port: 3000, // 端口
root: '', // 静态文件目录
index: 'html/index.html', // 入口文件
gzip: true, // 开启gzip压缩
compress: 'html|css|js', // 压缩这三种格式
openBrowser: true // 服务启动后自动打开浏览器
});*/