zzyxyz_server/server.ts
ZZY e3ead37760 feat(server): 添加类型定义和支持TypeScript的配置
已更新package.json以包含TypeScript编译步骤,并添加了对Express和Node.js的类型定义。这允许在项目中使用TypeScript编写代码,提供了更强的类型检查和更好的IDE集成体验。

此外,已删除多个不再需要的Node.js脚本和过时的控制器、服务和实用程序文件,以简化代码库并为其向TypeScript的迁移做准备。
2024-08-25 22:35:10 +08:00

42 lines
1.3 KiB
TypeScript
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.

import express, { Application, Request, Response, NextFunction } from 'express';
import path from 'path';
import dotenv from 'dotenv';
dotenv.config();
process.env.ROOT_DIR = path.resolve(__dirname);
const env = process.env;
export const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(require('cookie-parser')());
const dist = path.join(__dirname, env.STATIC_PATH ?? 'static');
// 静态资源服务假设你的dist目录包含了编译后的Vue应用
app.use(express.static(dist));
import router from './src/routes/api';
app.use('/api', router);
// // 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: Request, res: Response) => {
res.sendFile(path.join(dist, '/index.html'));
});
// error handler
app.use(function(err: any, req: Request, res: Response, next: NextFunction) {
// 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>');
});