-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
32 lines (27 loc) · 959 Bytes
/
server.js
File metadata and controls
32 lines (27 loc) · 959 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const express = require('express');
const path = require('path');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
const PORT = process.env.PORT || 3000;
// Serve static files from the dist directory
app.use(express.static(path.join(__dirname, 'dist')));
// Proxy API requests to another server
app.use('/api', createProxyMiddleware({
target: 'http://localhost:5001', // 目标服务器地址
changeOrigin: true,
pathRewrite: {
'^/': '/api/', // 保持路径不变
},
onProxyReq: (proxyReq, req, res) => {
// 打印转发的请求信息
console.log(`转发接口: ${req.originalUrl}`);
console.log(`请求方法: ${req.method}`);
},
}));
// Handle all other requests by sending the index.html file
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});