-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
147 lines (132 loc) · 4.28 KB
/
server.js
File metadata and controls
147 lines (132 loc) · 4.28 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env node
/**
* Beacon World App - Node.js Web服务器
* 用于本地运行Web版应用
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
// 配置
const PORT = 8080;
const HOST = 'localhost';
// MIME类型映射
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.wav': 'audio/wav',
'.mp4': 'video/mp4',
'.woff': 'application/font-woff',
'.ttf': 'application/font-ttf',
'.eot': 'application/vnd.ms-fontobject',
'.otf': 'application/font-otf',
'.wasm': 'application/wasm'
};
function getMimeType(filePath) {
const ext = path.extname(filePath).toLowerCase();
return mimeTypes[ext] || 'application/octet-stream';
}
function openBrowser(url) {
const start = (process.platform === 'darwin' ? 'open' :
process.platform === 'win32' ? 'start' : 'xdg-open');
exec(`${start} ${url}`);
}
// 创建HTTP服务器
const server = http.createServer((req, res) => {
// 添加CORS头部
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', '*');
// 处理预检请求
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
// 解析请求路径
let filePath = req.url === '/' ? '/index.html' : req.url;
filePath = path.join(__dirname, filePath);
// 检查文件是否存在
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
// 文件不存在,返回404
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end(`
<html>
<head><title>404 - 文件未找到</title></head>
<body>
<h1>404 - 文件未找到</h1>
<p>请求的文件不存在: ${req.url}</p>
<a href="/">返回首页</a>
</body>
</html>
`);
return;
}
// 读取并返回文件
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/html' });
res.end(`
<html>
<head><title>500 - 服务器错误</title></head>
<body>
<h1>500 - 服务器错误</h1>
<p>读取文件时发生错误</p>
</body>
</html>
`);
return;
}
// 设置正确的MIME类型
const mimeType = getMimeType(filePath);
res.writeHead(200, { 'Content-Type': mimeType });
res.end(data);
});
});
});
// 启动服务器
server.listen(PORT, HOST, () => {
console.log('='.repeat(50));
console.log(' Beacon World App - Web服务器');
console.log('='.repeat(50));
console.log();
console.log(`✅ 服务器启动成功!`);
console.log(`📍 地址: http://${HOST}:${PORT}`);
console.log(`📁 目录: ${__dirname}`);
console.log();
console.log('🌐 正在打开浏览器...');
console.log();
console.log('按 Ctrl+C 停止服务器');
console.log('='.repeat(50));
// 自动打开浏览器
setTimeout(() => {
openBrowser(`http://${HOST}:${PORT}`);
}, 1000);
});
// 处理服务器错误
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.log(`❌ 错误: 端口 ${PORT} 已被占用`);
console.log('请尝试:');
console.log('1. 关闭其他Web服务器');
console.log('2. 等待几秒后重试');
console.log('3. 重启计算机');
} else {
console.log(`❌ 服务器启动失败: ${err.message}`);
}
process.exit(1);
});
// 处理Ctrl+C
process.on('SIGINT', () => {
console.log();
console.log('🛑 服务器已停止');
console.log('感谢使用 Beacon World App!');
process.exit(0);
});