1 Star 0 Fork 0

wasleaf/MastersStudioManagementBackstage

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
app.js 3.27 KB
一键复制 编辑 原始数据 按行查看 历史
wasleaf 提交于 2024-05-20 09:23 . feat: 添加文件上传接口
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;
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/sssleaffffff/masters-studio-management-backstage.git
git@gitee.com:sssleaffffff/masters-studio-management-backstage.git
sssleaffffff
masters-studio-management-backstage
MastersStudioManagementBackstage
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385