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
const http = require('http');
 
// 自定义代理中间件,完全控制请求头
module.exports = {
  devServer: {
    port: 8080,
    open: true, 
    before: function(app) {
      app.post('/api/*', (req, res) => {
        const options = {
          hostname: '192.168.10.132',
          port: 8080,
          path: req.url.replace('/api', ''),
          method: 'POST',
          headers: {
            // 只复制必要的头部,完全模拟Postman
            'Content-Type': req.headers['content-type'],
            'Content-Length': req.headers['content-length'],
            'Host': '192.168.10.132:8080',
            'Connection': 'keep-alive',
            'User-Agent': 'Mozilla/5.0'
          }
        };
        
        const proxyReq = http.request(options, (proxyRes) => {
          res.writeHead(proxyRes.statusCode, proxyRes.headers);
          proxyRes.pipe(res);
        });
        
        proxyReq.on('error', (err) => {
          console.error('代理请求失败:', err);
          res.status(500).json({ error: '设备连接失败' });
        });
        
        req.pipe(proxyReq);
      });
    }
  }
};