import cors from 'cors' import mysql from 'mysql2' import express from 'express' import fs from 'fs' // 从 config.json 读取配置 const config = JSON.parse(fs.readFileSync('config.json', 'utf8')) const app = express() const db = mysql.createPool({ ...config.mysql, waitForConnections: true, connectionLimit: 10, queueLimit: 0 }).promise() // 设置JSON缩进为 2 个空格 app.set('json spaces', 2) // 允许所有域名的跨域请求 app.use(cors()) // 提供接口服务 app.get('/api', async (req, res) => { const [rows] = await db.query('SELECT * FROM flights LIMIT 10') res.json(rows) }) app.get('/api/metrics', async (req, res) => { const [rows] = await db.query('SELECT date, COUNT(*) AS count FROM flight_price_history GROUP BY date') res.json(rows) }) // 启动应用 app.listen(2000, () => { console.log('服务已启动成功 http://localhost:2000/api') })