This commit is contained in:
丹尼尔
2026-03-11 13:59:22 +08:00
parent 6da73da8d7
commit 152877cef2
18 changed files with 2930 additions and 33 deletions

View File

@@ -3,6 +3,7 @@ import cors from 'cors';
import morgan from 'morgan';
import dotenv from 'dotenv';
import path from 'path';
import fs from 'fs';
dotenv.config();
@@ -11,9 +12,19 @@ const backendHost = process.env.BACKEND_HOST || '127.0.0.1';
const backendPort = Number(process.env.BACKEND_PORT) || 8000;
const backendBase = `http://${backendHost}:${backendPort}`;
const logDir = process.env.LOG_DIR || path.join(__dirname, '../backend/data/logs');
try {
fs.mkdirSync(logDir, { recursive: true });
} catch {
// ignore
}
const accessLogPath = path.join(logDir, 'node-access.log');
const accessLogStream = fs.createWriteStream(accessLogPath, { flags: 'a' });
app.use(cors());
app.use(express.json());
app.use(morgan('dev'));
app.use(morgan('combined', { stream: accessLogStream }));
// 代理 /api 和 /auth 到后端,使前端用相对路径即可(任意 -p/-b 端口都正确)
async function proxyToBackend(req: express.Request, res: express.Response): Promise<void> {
@@ -54,8 +65,14 @@ app.get('/health', (_req, res) => {
const port = process.env.PORT || 3000;
app.listen(port, () => {
const msg = `Static frontend server listening on port ${port}; access log: ${accessLogPath}`;
// eslint-disable-next-line no-console
console.log(`Static frontend server listening on port ${port}`);
console.log(msg);
try {
accessLogStream.write(`[${new Date().toISOString()}] ${msg}\n`);
} catch {
// ignore
}
});