| | import express from 'express'
|
| | import { loadConfig, isDevMode, getPort } from './config.js'
|
| | import { logError } from './logger.js'
|
| | import router from './routes.js'
|
| |
|
| | const app = express();
|
| |
|
| | app.use(express.json({ limit: '50mb' }));
|
| | app.use(express.urlencoded({ extended: true, limit: '50mb' }));
|
| |
|
| | app.use((req, res, next) => {
|
| | res.header('Access-Control-Allow-Origin', '*');
|
| | res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
| | res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-API-Key, anthropic-version');
|
| |
|
| | if (req.method === 'OPTIONS') {
|
| | return res.sendStatus(200);
|
| | }
|
| | next();
|
| | });
|
| |
|
| | app.use(router);
|
| |
|
| | app.get('/', (req, res) => {
|
| | res.redirect('https://www.bilibili.com/video/BV1SMH5zfEwe/?spm_id_from=333.1007.tianma.1-1-1.click&vd_source=1f3b8eb28230105c578a443fa6481550')
|
| | })
|
| |
|
| |
|
| |
|
| | app.use((err, req, res, next) => {
|
| | logError('未处理的错误', err);
|
| | res.status(500).json({
|
| | error: '内部服务器错误',
|
| | message: isDevMode() ? err.message : undefined
|
| | });
|
| | });
|
| |
|
| | (async () => {
|
| | try {
|
| | loadConfig()
|
| | const PORT = getPort()
|
| |
|
| | const server = app.listen(PORT)
|
| | .on('listening', () => {
|
| | console.log(`服务器运行在 http://localhost:${PORT}`)
|
| | })
|
| | .on('error', (err) => {
|
| | if (err.code === 'EADDRINUSE') {
|
| | console.error(`\n${'='.repeat(80)}`);
|
| | console.error(`错误: 端口 ${PORT} 已被占用!`);
|
| | console.error('');
|
| | console.error('请选择以下选项之一:');
|
| | console.error(` 1. 停止使用端口 ${PORT} 的进程:`);
|
| | console.error(` lsof -ti:${PORT} | xargs kill`);
|
| | console.error('');
|
| | console.error(' 2. 使用环境变量更改端口:');
|
| | console.error(' export PORT=8080');
|
| | console.error(`${'='.repeat(80)}\n`);
|
| | process.exit(1);
|
| | } else {
|
| | logError('启动服务器失败', err);
|
| | process.exit(1);
|
| | }
|
| | });
|
| | } catch (error) {
|
| | logError('启动服务器失败', error);
|
| | process.exit(1);
|
| | }
|
| | })();
|
| |
|