zzyxyz_server/server.js
ZZY 510a33489f refactor(server): 整理文件路由和处理逻辑
重构文件服务相关的路由和控制器。移除了未使用的路由和控制器文件,简化了文件服务逻辑。现在,路由更加清晰且控制器逻辑更加集中。此外,更新了.gitignore文件以排除不必要的文件和文件夹,并调整了默认环境变量以适应新的文件路径结构。start.py脚本中自动化构建和启动流程已被移除,以适应手动构建和启动服务器的新流程。
2024-08-25 11:10:19 +08:00

41 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const express = require('express');
const path = require('path');
require('dotenv').config();
env = process.env;
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(require('cookie-parser')());
dist = path.join(__dirname, env.STATIC_PATH ?? 'static')
// 静态资源服务假设你的dist目录包含了编译后的Vue应用
app.use(express.static(dist));
app.use('/api', require('./routes/api.js'));
// // catch 404 and forward to error handler
// app.use(function(req, res, next) {
// res.status(404);
// res.end('<h1>404 Not Found</h1>');
// });
// 所有未匹配到的请求都重定向到index.html,使用vue3的SPA匹配
app.get('*', (req, res) => {
res.sendFile(path.join(dist, '/index.html'));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = env.MOD === 'development' ? err : {};
console.log(err);
// render the error page
res.status(err.status || 500);
res.end('<h1>error</h1>');
});
module.exports = app;