133 lines
3.4 KiB
JavaScript
133 lines
3.4 KiB
JavaScript
const express = require('express');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const cors = require('cors');
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3001;
|
|
|
|
// 中间件
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
app.use(express.static('dist'));
|
|
|
|
// 统计文件路径
|
|
const STATS_FILE = '/www/stat';
|
|
const LOCAL_STATS_FILE = path.join(__dirname, 'stats.json');
|
|
|
|
// 确保统计文件存在
|
|
function ensureStatsFile() {
|
|
const statsFile = fs.existsSync('/www') ? STATS_FILE : LOCAL_STATS_FILE;
|
|
|
|
if (!fs.existsSync(statsFile)) {
|
|
const initialStats = {
|
|
visits: 0,
|
|
lastUpdated: new Date().toISOString()
|
|
};
|
|
|
|
try {
|
|
if (statsFile === STATS_FILE) {
|
|
// 如果是服务器路径,确保目录存在
|
|
if (!fs.existsSync('/www')) {
|
|
fs.mkdirSync('/www', { recursive: true });
|
|
}
|
|
}
|
|
fs.writeFileSync(statsFile, JSON.stringify(initialStats, null, 2));
|
|
} catch (error) {
|
|
console.error('创建统计文件失败:', error);
|
|
}
|
|
}
|
|
|
|
return statsFile;
|
|
}
|
|
|
|
// 读取统计数据
|
|
function readStats() {
|
|
const statsFile = ensureStatsFile();
|
|
|
|
try {
|
|
const data = fs.readFileSync(statsFile, 'utf8');
|
|
return JSON.parse(data);
|
|
} catch (error) {
|
|
console.error('读取统计数据失败:', error);
|
|
return { visits: 0, lastUpdated: new Date().toISOString() };
|
|
}
|
|
}
|
|
|
|
// 写入统计数据
|
|
function writeStats(stats) {
|
|
const statsFile = ensureStatsFile();
|
|
|
|
try {
|
|
fs.writeFileSync(statsFile, JSON.stringify(stats, null, 2));
|
|
return true;
|
|
} catch (error) {
|
|
console.error('写入统计数据失败:', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// API路由 - 记录访问
|
|
app.post('/api/visit', (req, res) => {
|
|
try {
|
|
const stats = readStats();
|
|
stats.visits += 1;
|
|
stats.lastUpdated = new Date().toISOString();
|
|
|
|
const success = writeStats(stats);
|
|
|
|
if (success) {
|
|
res.json({
|
|
success: true,
|
|
visits: stats.visits,
|
|
message: '访问统计已更新'
|
|
});
|
|
} else {
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '统计数据写入失败'
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error('处理访问统计失败:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '服务器内部错误'
|
|
});
|
|
}
|
|
});
|
|
|
|
// API路由 - 获取统计数据
|
|
app.get('/api/stats', (req, res) => {
|
|
try {
|
|
const stats = readStats();
|
|
res.json({
|
|
success: true,
|
|
data: stats
|
|
});
|
|
} catch (error) {
|
|
console.error('获取统计数据失败:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '获取统计数据失败'
|
|
});
|
|
}
|
|
});
|
|
|
|
// 服务静态文件
|
|
app.get('*', (req, res) => {
|
|
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
|
|
});
|
|
|
|
// 启动服务器
|
|
app.listen(PORT, () => {
|
|
console.log(`服务器运行在端口 ${PORT}`);
|
|
console.log(`访问地址: http://localhost:${PORT}`);
|
|
|
|
// 初始化统计文件
|
|
ensureStatsFile();
|
|
const stats = readStats();
|
|
console.log(`当前访问统计: ${stats.visits} 次`);
|
|
});
|
|
|
|
module.exports = app;
|