You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Artillery压测Deno服务器时出现errors.ECONNREFUSED错误求助

解决方案:解决Deno+Express应用Artillery压测时ECONNREFUSED错误

1. 修正Express监听地址,确保服务可被正常访问

默认情况下,Express在Deno环境中app.listen(3000)可能仅绑定IPv4的127.0.0.1,而部分环境中localhost会优先解析为IPv6的::1,导致Artillery无法建立连接。修改监听地址为0.0.0.0,让服务监听所有网络接口:

app.listen(3000, '0.0.0.0', () => console.log('Server is running on http://0.0.0.0:3000'));

同时可将Artillery配置文件的target改为http://0.0.0.0:3000,绑定0.0.0.0也兼容localhost的解析请求。

2. 修复MongoDB连接的错误处理逻辑

你的代码中Mongo连接的catch块存在语法错误,当前写法会直接执行console.log而非在错误发生时触发,导致无法正确捕获连接异常:

// 错误写法
client.connect().then(() => console.log("Connected successfully to server")).catch(console.log("Error connecting to server"));

// 修正后
client.connect()
  .then(() => console.log("Connected successfully to server"))
  .catch(err => console.log("Error connecting to server", err));

这虽不是本次ECONNREFUSED的直接原因,但会导致无法排查Mongo连接相关的潜在问题。

3. 确保Deno运行时权限正确

运行Deno应用时必须授予网络访问权限,否则无法监听端口或连接MongoDB,启动命令需包含--allow-net:

deno run --allow-net index.ts

4. 验证服务启动状态

启动服务后,先通过curl或Postman确认服务正常响应,再启动Artillery压测,避免服务未完全就绪时就发起请求:

curl -X POST http://localhost:3000 -H "Content-Type: application/json" -d '{"name":"John","surname":"Doe","birthday":"1990-01-01"}'

若以上步骤执行后仍有问题,可排查:

  • 端口3000是否被其他进程占用:Linux/macOS用lsof -i :3000,Windows用netstat -ano | findstr :3000。
  • Artillery版本兼容性:尝试更新到最新版本,执行npm update -g artillery。

内容的提问来源于stack exchange,提问作者anileates

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 09:01:27