41 lines
1.1 KiB
JavaScript
41 lines
1.1 KiB
JavaScript
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, 'vue-project', 'dist')
|
||
// 静态资源服务,假设你的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; |