-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-server.js
More file actions
335 lines (286 loc) · 9.93 KB
/
basic-server.js
File metadata and controls
335 lines (286 loc) · 9.93 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');
// 简单的内存存储
let adminPassword = 'admin123';
const sessions = new Map();
// 生成会话ID
function generateSessionId() {
return Math.random().toString(36).substring(2) + Date.now().toString(36);
}
// 获取文件MIME类型
function getMimeType(filePath) {
const ext = path.extname(filePath).toLowerCase();
const mimeTypes = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon'
};
return mimeTypes[ext] || 'text/plain';
}
// 解析JSON请求体
function parseJSON(req, callback) {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
try {
const data = JSON.parse(body);
callback(null, data);
} catch (error) {
callback(error, null);
}
});
}
// 身份验证中间件
function requireAuth(req, res, next) {
const sessionId = req.headers['x-session-id'];
if (!sessionId || !sessions.has(sessionId)) {
res.writeHead(401, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Unauthorized' }));
return;
}
const session = sessions.get(sessionId);
if (Date.now() - session.loginTime > 24 * 60 * 60 * 1000) {
sessions.delete(sessionId);
res.writeHead(401, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Session expired' }));
return;
}
next();
}
// 模拟文件数据
const sampleFiles = [
{
id: 1,
name: 'Node.js 开发指南',
description: 'Node.js 完整开发教程',
url: 'https://example.com/nodejs-guide.pdf',
category: '编程教程',
size: 5242880,
type: 'pdf',
downloads: 15,
created_at: '2024-01-15T10:30:00Z'
},
{
id: 2,
name: 'React 实战项目',
description: 'React 前端框架实战案例',
url: 'https://example.com/react-project.zip',
category: '前端开发',
size: 10485760,
type: 'zip',
downloads: 23,
created_at: '2024-01-20T14:20:00Z'
}
];
let files = [...sampleFiles];
let nextId = 3;
// 创建HTTP服务器
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
const pathname = parsedUrl.pathname;
const method = req.method;
// 设置CORS头
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, X-Session-ID');
// 处理OPTIONS请求
if (method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
// API路由处理
if (pathname.startsWith('/api/')) {
// 登录接口
if (pathname === '/api/login' && method === 'POST') {
parseJSON(req, (err, data) => {
if (err) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: false, message: '请求格式错误' }));
return;
}
const { username, password } = data;
if (!username || !password) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: false, message: '用户名和密码不能为空' }));
return;
}
if (username === 'admin' && password === adminPassword) {
const sessionId = generateSessionId();
sessions.set(sessionId, {
username,
loginTime: Date.now()
});
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
success: true,
sessionId,
username
}));
} else {
res.writeHead(401, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
success: false,
message: '用户名或密码错误'
}));
}
});
return;
}
// 验证会话
if (pathname === '/api/auth/status' && method === 'GET') {
const sessionId = req.headers['x-session-id'];
if (!sessionId || !sessions.has(sessionId)) {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ authenticated: false }));
return;
}
const session = sessions.get(sessionId);
if (Date.now() - session.loginTime > 24 * 60 * 60 * 1000) {
sessions.delete(sessionId);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ authenticated: false }));
return;
}
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ authenticated: true, username: session.username }));
return;
}
// 修改密码
if (pathname === '/api/change-password' && method === 'POST') {
requireAuth(req, res, () => {
parseJSON(req, (err, data) => {
if (err) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: false, message: '请求格式错误' }));
return;
}
const { currentPassword, newPassword } = data;
if (!currentPassword || !newPassword) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: false, message: '请填写完整信息' }));
return;
}
if (newPassword.length < 6) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: false, message: '新密码长度至少6位' }));
return;
}
if (currentPassword !== adminPassword) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: false, message: '当前密码错误' }));
return;
}
// 更新密码
adminPassword = newPassword;
// 清除所有会话
sessions.clear();
console.log(`✅ 密码已更新为: ${newPassword}`);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: true, message: '密码修改成功,请重新登录' }));
});
});
return;
}
// 获取文件列表
if (pathname === '/api/files' && method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(files));
return;
}
// 添加文件
if (pathname === '/api/files' && method === 'POST') {
requireAuth(req, res, () => {
parseJSON(req, (err, data) => {
if (err) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: '请求格式错误' }));
return;
}
const { name, description, url, category, size, type } = data;
if (!name || !url) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Name and URL are required' }));
return;
}
const newFile = {
id: nextId++,
name,
description: description || '',
url,
category: category || 'uncategorized',
size: parseInt(size) || 0,
type: type || '',
downloads: 0,
created_at: new Date().toISOString()
};
files.push(newFile);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: true }));
});
});
return;
}
// 删除文件
if (pathname.startsWith('/api/files/') && method === 'DELETE') {
requireAuth(req, res, () => {
const id = parseInt(pathname.split('/')[3]);
const index = files.findIndex(f => f.id === id);
if (index === -1) {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'File not found' }));
return;
}
files.splice(index, 1);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: true }));
});
return;
}
// 获取分类列表
if (pathname === '/api/categories' && method === 'GET') {
const categories = [...new Set(files.map(f => f.category))];
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(categories));
return;
}
// 404 for unknown API routes
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'API endpoint not found' }));
return;
}
// 静态文件服务
let filePath = path.join(__dirname, 'public', pathname === '/' ? 'index.html' : pathname);
fs.readFile(filePath, (err, content) => {
if (err) {
if (err.code === 'ENOENT') {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<h1>404 - File Not Found</h1>');
} else {
res.writeHead(500, { 'Content-Type': 'text/html' });
res.end('<h1>500 - Internal Server Error</h1>');
}
} else {
const mimeType = getMimeType(filePath);
res.writeHead(200, { 'Content-Type': mimeType });
res.end(content);
}
});
});
const port = 3000;
server.listen(port, () => {
console.log(`🚀 开发服务器启动在 http://localhost:${port}`);
console.log(`📱 管理后台: http://localhost:${port}/admin.html`);
console.log(`🔑 默认账户: admin / admin123`);
console.log(`💡 提示: 修改密码后会在控制台显示新密码`);
});