61 lines
1.4 KiB
TypeScript
61 lines
1.4 KiB
TypeScript
import express, { Express, Request, Response } from 'express';
|
|
import cors from 'cors';
|
|
import dotenv from 'dotenv';
|
|
import { AppDataSource } from './config/database';
|
|
import adminRoutes from './routes/adminRoutes';
|
|
import playerRoutes from './routes/playerRoutes';
|
|
|
|
dotenv.config();
|
|
|
|
const app: Express = express();
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
app.use(cors({
|
|
origin: ['http://localhost:5173', 'http://localhost:5174'],
|
|
credentials: true
|
|
}));
|
|
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
app.use('/api/admin', adminRoutes);
|
|
app.use('/api/player', playerRoutes);
|
|
|
|
app.get('/', (req: Request, res: Response) => {
|
|
res.json({
|
|
message: '梦幻西游一站式运营管理平台 API 服务',
|
|
version: '1.0.0'
|
|
});
|
|
});
|
|
|
|
app.use((req: Request, res: Response) => {
|
|
res.status(404).json({
|
|
success: false,
|
|
message: '接口不存在'
|
|
});
|
|
});
|
|
|
|
app.use((err: any, req: Request, res: Response, next: any) => {
|
|
console.error('服务器错误:', err);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '服务器内部错误'
|
|
});
|
|
});
|
|
|
|
const startServer = async () => {
|
|
try {
|
|
await AppDataSource.initialize();
|
|
console.log('数据库连接成功');
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`服务器运行在 http://localhost:${PORT}`);
|
|
});
|
|
} catch (error) {
|
|
console.error('服务器启动失败:', error);
|
|
process.exit(1);
|
|
}
|
|
};
|
|
|
|
startServer();
|