123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import express from "express";
- import { serverConfig } from "./config.js";
- import { logger } from "./middleware/logger.js";
- import { errorHandler } from "./middleware/errorHandler.js";
- import exampleRoutes from "./routes/exampleRoutes.js";
- import chartRoutes from "./routes/chartRoutes.js";
- import path from "path";
- import { fileURLToPath } from "url";
- const __filename = fileURLToPath(import.meta.url);
- const __dirname = path.dirname(__filename);
- const app = express();
- // 中间件
- app.use(express.json());
- app.use(logger);
- // 静态文件服务
- app.use("/images", express.static(path.join(process.cwd(), "src", "images")));
- app.use("/js", express.static(path.join(process.cwd(), "src", "public", "js")));
- app.use(
- "/testData.json",
- express.static(path.join(process.cwd(), "src", "testData.json"))
- );
- // 路由
- app.use("/api/examples", exampleRoutes);
- app.use("/api/charts", chartRoutes);
- // 错误处理
- app.use(errorHandler);
- export const startServer = () => {
- app.listen(serverConfig.port, serverConfig.host, () => {
- console.log(
- `Server is running on http://${serverConfig.host}:${serverConfig.port}`
- );
- });
- };
- export default app;
|