解决vite proxy error

1,167 阅读1分钟

问题

使用vite构建的前端,页面加载时候请求后端接口,接口返回超时。

本人用的是mac,其他同事使用windows正常。

ping后端的ip能ping通

报错

[vite] http proxy error at xx/xxx:
Error: connect ETIMEDOUT xxxx:xxxx
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16)

解决

参考:github.com/vitejs/vite…

vite proxy 添加

import { Agent } from 'node:http';

'/api' = {
      target: target,
      changeOrigin: true,
      ws: true,
      agent: new Agent({ keepAlive: true, keepAliveMsecs: 20000 }), // 加这一行
    };