代码拉取完成,页面将自动刷新
var createError = require("http-errors");
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
var mysql = require("mysql2");
var swaggerJsdoc = require("swagger-jsdoc");
var swaggerUi = require("swagger-ui-express");
// Swagger 配置
var swaggerOptions = {
definition: {
openapi: "3.0.0",
info: {
title: "API 文档",
version: "1.0.0",
},
},
apis: ["./routes/*.js"], // 指定包含 API 路由的文件路径
};
var swaggerSpec = swaggerJsdoc(swaggerOptions);
var indexRouter = require("./routes/index");
var usersRouter = require("./routes/users");
var equipmentRouter = require("./routes/equipment");
var directionRouter = require("./routes/direction");
var taskRouter = require("./routes/task");
var stepRouter = require("./routes/step");
var messageRouter = require("./routes/message");
var joinRouter = require("./routes/join");
var borrowRouter = require("./routes/borrow");
var achievementsRouter = require("./routes/achievements");
var uploadRouter = require("./routes/upload");
var app = express();
var pool = mysql.createPool({
host: "localhost",
user: "root",
password: "020129",
database: "studio",
});
// 测试连接
pool.getConnection((err, connection) => {
if (err) {
console.error("数据库连接失败:", err);
} else {
console.log("数据库连接成功");
connection.release(); // 释放连接
}
});
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "jade");
app.set("db", pool);
// todo 身份验证的中间件
// 自定义中间件,用于统一处理返回值格式
app.use((req, res, next) => {
const originalJson = res.json; // 保存原始的 res.json 方法
// 重写 res.json 方法,实现格式化和统一处理
res.json = function (data) {
const formattedData = {
success: true,
data,
};
res.setHeader("Content-Type", "application/json");
originalJson.call(res, formattedData); // 调用原始的 res.json 方法
};
next();
});
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
app.use("/", indexRouter);
app.use("/users", usersRouter);
app.use("/equipment", equipmentRouter);
app.use("/direction", directionRouter);
app.use("/task", taskRouter);
app.use("/step", stepRouter);
app.use("/message", messageRouter);
app.use("/join", joinRouter);
app.use("/borrow", borrowRouter);
app.use("/achievements", achievementsRouter);
app.use("/upload", uploadRouter);
// 将 Swagger UI 添加到 Express 应用中
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
// catch 404 and forward to error handler
app.use(function (req, res, next) {
res.status(500).json({
success: false,
error: "服务器内部错误",
});
// next(createError(404));
});
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
res.render("error");
});
console.log("=====服务启动完毕=====");
module.exports = app;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。