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

使用Axios向Express服务器发字符串数组遇URL及ECONNRESET错误求助

问题:通过localhost:3000/start发送字符串数组并获取带Map的响应

需求

我希望通过localhost:3000/start路由发送一个字符串数组,服务器返回200状态码,且响应体中附带一个Map。

现有代码

客户端代码

const axios = require('axios');
let listOfNames = ['mikey'];
axios.post(''http://localhost:3000/start'', {
        data: { names: listOfNames }
      })
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      });

服务器代码

const express = require('express');
const app = express()
const port = 3000
var listOfNames = [];
app.post('/start', async (req, res) => {
  listOfNames = req.params.listOfNames;
  res.status(200).send("Names added");
});

app.listen(port, () => {
  console.log('request recieved');
});

遇到的错误

第一个错误:无效URL

TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:393:5)
    at URL.onParseError (node:internal/url:565:9)
    at new URL (node:internal/url:645:5)
    at dispatchHttpRequest (C:\Users\cmb\rectangleHealth\node_modules\axios\dist\node\axios.cjs:2394:20)
    at new Promise (<anonymous>)
    at http (C:\Users\cmb\rectangleHealth\node_modules\axios\dist\node\axios.cjs:2330:10)
    at Axios.dispatchRequest (C:\Users\cmb\rectangleHealth\node_modules\axios\dist\node\axios.cjs:3260:10)
    at Axios.request (C:\Users\cmb\rectangleHealth\node_modules\axios\dist\node\axios.cjs:3610:33)
    at Axios.httpMethod [as post] (C:\Users\cmb\rectangleHealth\node_modules\axios\dist\node\axios.cjs:3649:19)
    at Function.wrap [as post] (C:\Users\cmb\rectangleHealth\node_modules\axios\dist\node\axios.cjs:27:15) {
  input: '/start',
  code: 'ERR_INVALID_URL'
}

修复后新错误:连接重置

AxiosError: read ECONNRESET
    at AxiosError.from (C:\Users\cmb\rectangleHealth\node_modules\axios\dist\node\axios.cjs:789:14)
    at RedirectableRequest.handleRequestError (C:\Users\cmb\rectangleHealth\node_modules\axios\dist\node\axios.cjs:2744:25)
    at RedirectableRequest.emit (node:events:513:28)
    at eventHandlers.<computed> (C:\Users\cmb\rectangleHealth\node_modules\follow-redirects\index.js:14:24)
    at ClientRequest.emit (node:events:513:28)
    at Socket.socketErrorListener (node:_http_client:494:9)
    at Socket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  syscall: 'read',
  code: 'ECONNRESET',
  errno: -4077,
}

控制台还输出了无法展示的request和config JSON对象。


问题分析与修复方案

1. 修复客户端URL与请求体格式问题

  • URL的双引号嵌套语法错误:把''http://localhost:3000/start''改成'http://localhost:3000/start'
  • Axios的post方法第二个参数直接是请求体数据,不需要嵌套在data字段里

修复后的客户端代码:

const axios = require('axios');
let listOfNames = ['mikey'];
axios.post('http://localhost:3000/start', { names: listOfNames })
  .then(function (response) {
    console.log(response.data);
  })
  .catch(function (error) {
    console.log(error);
  });

2. 修复服务器请求体解析与数据获取问题

  • Express默认不解析JSON请求体,需要添加express.json()中间件
  • 服务器错误使用了req.params获取数据,应该通过req.body读取请求体中的数组
  • 实现响应体附加Map的需求:将Map转换为普通对象(直接返回Map会被序列化为空对象)

修复后的服务器代码:

const express = require('express');
const app = express();
const port = 3000;

// 启用JSON请求体解析
app.use(express.json());

var listOfNames = [];

app.post('/start', async (req, res) => {
  // 从请求体获取names数组
  listOfNames = req.body.names || [];
  
  // 创建并填充Map
  const nameMap = new Map();
  listOfNames.forEach(name => {
    nameMap.set(name, true); // 示例:将名字作为键,值设为true
  });
  
  // 返回200状态码,响应体包含消息和转换后的Map对象
  res.status(200).json({
    message: "Names added",
    nameMap: Object.fromEntries(nameMap)
  });
});

app.listen(port, () => {
  console.log(`服务器运行在 http://localhost:${port}`);
});

3. 解决ECONNRESET错误

这个错误通常是因为客户端请求时服务器未启动,或端口被占用。解决步骤:

  • 先启动服务器,再运行客户端代码
  • 检查3000端口是否被其他服务占用,必要时更换端口

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 00:55:12