-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcraco.config.js
More file actions
52 lines (43 loc) · 1.43 KB
/
craco.config.js
File metadata and controls
52 lines (43 loc) · 1.43 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// craco 具体的配置,只给开发环境使用
const http = require('http');
const https = require('https');
const { URL } = require('url');
const fs = require('fs');
const path = require('path');
module.exports = {
devServer: (devServerConfig, { env, paths, proxy, allowedHost }) => {
devServerConfig.onBeforeSetupMiddleware = (devServer) => {
// get/post /api/config...
devServer.app.use('/api', async (req, res) => {
const proxyReq = http.request({
hostname: 'localhost',
port: 8003,
path: '/api' + req.url, // 关键修改:添加缺失的 /api 前缀
method: req.method,
headers: req.headers
}, (proxyRes) => {
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res);
});
proxyReq.on('error', (err) => {
console.error('Proxy error:', err);
res.status(500).send('Proxy Error');
});
req.pipe(proxyReq);
});
};
/*
// 保存原始的onListening回调(如果存在)
const originalOnListening = devServerConfig.onListening;
// 在服务监听完成后启动LocalProxy
devServerConfig.onListening = (devServer) => {
// 调用原始的onListening(如果存在)
if (originalOnListening) {
originalOnListening(devServer);
}
// LocalProxy.init();
};
*/
return devServerConfig;
},
};