1.2 修改server.js
添加分页查询API的入口
JavaScript
运行代码
复制代码
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
// server.js - Web 服务器,提供静态文件服务
// 提供静态文件服务(CSS、JS、图片等)
app.use(express.static(path.join(__dirname, 'public')));
// 主页路由
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// API: 获取所有筛选选项
app.get('/api/filter-options', (req, res) => {
try {
const options = getAllFilterOptions();
res.json({
success: true,
data: options
});
} catch (error) {
res.status(500).json({
success: false,
message: '获取筛选选项失败',
error: error.message
});
}
});
// API: 分页查询武器皮肤
app.get('/api/weapon-skins', async (req, res) => {
try {
const {
page = 1,
pageSize = 12,
minPrice,
maxPrice,
appearance,
category,
quality,
isCollectible,
baseWeapon
} = req.query;
// 处理数组参数
const parseArrayParam = (param) => {
if (!param) return [];
if (Array.isArray(param)) return param;
if (typeof param === 'string') {
try {
const parsed = JSON.parse(param);
return Array.isArray(parsed) ? parsed : [parsed];
} catch {
return param.split(',').filter(Boolean);
}
}
return [];
};
const options = {
page: parseInt(page),
pageSize: parseInt(pageSize),
minPrice: minPrice || null,
maxPrice: maxPrice || null,
appearance: parseArrayParam(appearance),
category: parseArrayParam(category),
quality: parseArrayParam(quality),
isCollectible: isCollectible !== undefined ? (isCollectible === 'true' || isCollectible === '1') : null,
baseWeapon: parseArrayParam(baseWeapon)
};
const result = await weaponSkinModel.getWeaponSkinsWithPagination(options);
res.json(result);
} catch (error) {
res.status(500).json({
success: false,
message: '查询武器皮肤失败',
error: error.message
});
}
});
// 启动服务器
app.listen(PORT, () => {
console.log(服务器运行在 http://localhost:${PORT});
console.log('请在浏览器中打开上述地址查看页面');
});