2026-03-11 05:14:48 +08:00

74 lines
1.7 KiB
JavaScript

import express from 'express'
import cors from 'cors'
import {
ensureDataFiles,
readAllYearsData,
readYearData,
readStatus
} from './lib/cache.js'
const app = express()
app.use(cors())
app.use(express.json())
ensureDataFiles()
app.get('/api/qishier/all', (_req, res) => {
try {
const cache = readAllYearsData()
res.json({
updatedAt: cache.updatedAt,
name: cache.name || '七十二家房客',
coverUrl: cache.coverUrl || '',
displayType: cache.displayType || 0,
total: cache.items?.length || 0,
years: cache.years || [],
list: cache.items || []
})
} catch (error) {
res.status(500).json({
message: '读取缓存失败',
error: error.message
})
}
})
app.get('/api/qishier/year/:year', (req, res) => {
try {
const year = Number(req.params.year)
const data = readYearData(year)
res.json({
updatedAt: data.updatedAt,
name: '七十二家房客',
coverUrl: '',
displayType: 0,
total: data.items?.length || 0,
years: [year],
list: data.items || []
})
} catch (error) {
res.status(500).json({
message: '读取年度缓存失败',
error: error.message
})
}
})
app.get('/api/qishier/status', (_req, res) => {
try {
const status = readStatus()
res.json(status)
} catch (error) {
res.status(500).json({
message: '读取状态失败',
error: error.message
})
}
})
app.listen(3000, () => {
console.log('Node 服务已启动: http://localhost:3000')
})