Nuxt 3 SSR服务端请求报错:connect ECONNREFUSED 127.0.0.1:28960
Nuxt 3 SSR项目调用服务时出现连接拒绝错误的排查方案
问题描述
正在开发一个Nuxt 3 SSR项目,全程依赖服务端逻辑、无需客户端执行JS,但调用内部关联服务的Store时,反复触发连接拒绝错误,已尝试多平台方案无效,使用Node.js 20版本。
相关代码
import { AccountService, Activate2FaResponseDTO, OpenAPI, Pgp2FactorAuthenticationDTO } from "~/services/market-place"; export default defineEventHandler(async (event) => { const config = useRuntimeConfig(event); OpenAPI.BASE = config.public.restApiBaseUrl; console.log("OpenAPI.BASE", OpenAPI.BASE); const body = await readBody(event); const { publicKey } = body; if (!isPgpKey(publicKey)) { return sendRedirect(event, "/pgp/SendPgpKey?error=true"); } else if (isPgpKey(publicKey)) { console.log("okay bro"); const data: Pgp2FactorAuthenticationDTO = { publicKey }; console.log(data); const response: Activate2FaResponseDTO = await AccountService.create2FaAuthentication({ requestBody: data }); if (response && response.sessionId && response.encryptedCode) { console.log("Service response:", JSON.stringify(response, null, 2)); return response; } else { return sendRedirect(event, "/pgp/SendPgpKey?error=true"); } } else { return sendRedirect(event, "/pgp/SendPgpKey?error=true"); } });
报错信息
ERROR [nuxt] [request error] [unhandled] [500] connect ECONNREFUSED 127.0.0.1:28960
排查与解决方法
1. 验证API地址配置有效性
- 确认
config.public.restApiBaseUrl的实际值,若目标服务部署在外部服务器,绝对不能用127.0.0.1:28960这类本地回环地址,必须替换为公网可访问的域名/IP。 - 若目标API仅服务端调用,建议改用
runtimeConfig.api而非public配置,避免前端泄露敏感地址,同时确保服务端能正确读取到配置值。
2. 检查目标服务运行状态
- 确认28960端口对应的服务是否正常启动,且监听地址不是仅绑定
127.0.0.1(若服务部署在容器/远程服务器,需改为0.0.0.0允许外部访问)。 - 在Nuxt项目所在服务器上,用命令测试连通性:
若连接失败,优先排查目标服务本身的启动状态或端口配置。curl http://目标服务地址:28960/对应接口路径 telnet 目标服务地址 28960
3. 修复OpenAPI客户端全局配置冲突
- 当前代码直接修改全局的
OpenAPI.BASE,并发请求场景下会被覆盖,导致后续请求指向错误地址。建议每次请求创建独立客户端实例:// 替代全局修改,生成独立配置的客户端 const apiConfig = new Configuration({ basePath: config.public.restApiBaseUrl }); const accountService = new AccountService(apiConfig); const response = await accountService.create2FaAuthentication({ requestBody: data });
4. 排查网络与防火墙限制
- 检查Nuxt服务器与目标API服务器之间的防火墙规则,确保28960端口已开放。
- 若使用Docker或容器部署,确认容器端口映射是否正确,容器网络是否能访问目标服务所在的网络环境。
5. 验证Node.js版本兼容性
- Node.js 20虽稳定,但部分自动生成的OpenAPI客户端可能存在适配问题,可临时降级到Node.js 18 LTS版本测试,排除版本兼容导致的异常。
内容的提问来源于stack exchange,提问作者David Kuepo
相关产品推荐
相关产品推荐

