-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
109 lines (92 loc) · 2.81 KB
/
server.js
File metadata and controls
109 lines (92 loc) · 2.81 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
const http = require('http');
const fs = require('fs');
const path = require('path');
/**
* 加载 .env 文件中的环境变量
*/
function loadEnvFile() {
const envPath = path.join(__dirname, '.env');
if (fs.existsSync(envPath)) {
const envContent = fs.readFileSync(envPath, 'utf-8');
envContent.split('\n').forEach(line => {
const trimmed = line.trim();
if (trimmed && !trimmed.startsWith('#')) {
const [key, ...valueParts] = trimmed.split('=');
if (key && valueParts.length > 0) {
const value = valueParts.join('=').trim();
process.env[key.trim()] = value;
}
}
});
console.log('✅ 已加载 .env 文件');
}
}
loadEnvFile();
const handler = require('./api/count');
const PORT = process.env.PORT || 3000;
/**
* 解析 URL 查询参数
* @param {string} search - URL 查询字符串
* @returns {Object} 查询参数对象
*/
function parseQuery(search) {
const params = {};
if (!search) return params;
const searchParams = new URLSearchParams(search);
for (const [key, value] of searchParams) {
params[key] = value;
}
return params;
}
/**
* 本地开发服务器
*/
const server = http.createServer(async (req, res) => {
const baseUrl = `http://localhost:${PORT}`;
const parsedUrl = new URL(req.url, baseUrl);
const pathname = parsedUrl.pathname;
console.log(`[${new Date().toISOString()}] ${req.method} ${pathname}`);
if (pathname === '/') {
const indexPath = path.join(__dirname, 'index.html');
fs.readFile(indexPath, (err, data) => {
if (err) {
res.writeHead(500);
res.end('Error loading index.html');
return;
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
});
return;
}
const countMatch = pathname.match(/^\/([^/]+)\/count\.svg$/);
if (countMatch) {
req.query = parseQuery(parsedUrl.search);
req.query.username = countMatch[1];
const chunks = [];
for await (const chunk of req) {
chunks.push(chunk);
}
req.body = Buffer.concat(chunks).toString();
await handler(req, res);
return;
}
if (pathname.startsWith('/api/')) {
req.query = parseQuery(parsedUrl.search);
const chunks = [];
for await (const chunk of req) {
chunks.push(chunk);
}
req.body = Buffer.concat(chunks).toString();
await handler(req, res);
return;
}
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Not Found' }));
});
server.listen(PORT, () => {
console.log(`\n🚀 本地开发服务器已启动!`);
console.log(`📍 访问地址: http://localhost:${PORT}`);
console.log(`📊 测试地址: http://localhost:${PORT}/testuser/count.svg\n`);
console.log(`💡 提示: 请确保已设置 MONGODB_URI 环境变量\n`);
});