-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.js
More file actions
75 lines (65 loc) · 2.09 KB
/
serve.js
File metadata and controls
75 lines (65 loc) · 2.09 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
#!/usr/bin/env node
/**
* 简单的HTTP服务器,用于本地预览静态博客
* 使用方法:node serve.js
* 然后在浏览器访问 http://localhost:8000
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const PORT = 8000;
// MIME类型映射
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.md': 'text/markdown',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif',
'.ico': 'image/x-icon'
};
const server = http.createServer((req, res) => {
let filePath = '.' + req.url;
if (filePath === './') {
filePath = './index.html';
}
const extname = String(path.extname(filePath)).toLowerCase();
const mimeType = mimeTypes[extname] || 'application/octet-stream';
fs.readFile(filePath, (error, content) => {
if (error) {
if (error.code === 'ENOENT') {
res.writeHead(404);
res.end('File not found');
} else {
res.writeHead(500);
res.end('Server error: ' + error.code);
}
} else {
res.writeHead(200, { 'Content-Type': mimeType });
res.end(content, 'utf-8');
}
});
});
server.listen(PORT, () => {
console.log(`启动本地服务器...`);
console.log(`端口: ${PORT}`);
console.log(`目录: ${process.cwd()}`);
console.log(`服务器运行在 http://localhost:${PORT}`);
console.log('按 Ctrl+C 停止服务器');
// 自动打开浏览器
const url = `http://localhost:${PORT}`;
const command = process.platform === 'win32' ? 'start' :
process.platform === 'darwin' ? 'open' : 'xdg-open';
exec(`${command} ${url}`);
});
server.on('error', (e) => {
if (e.code === 'EADDRINUSE') {
console.log(`端口 ${PORT} 已被占用,请尝试其他端口`);
console.log('或者关闭占用该端口的程序');
} else {
console.log('启动服务器失败:', e);
}
});