繁体   English   中英

使用 axios 向网络服务器发送请求

[英]Sending request to webserver using axios

我想通过路由start在本地主机 3000 上发送一个字符串数组,然后发回状态为 200 的响应,最终将 map 附加到 response.body 目前我有这个客户端代码:

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');
});

我大概是从请求的发送方式中得到这个错误的,有什么帮助吗?

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:23
94: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.cj
s:27:15) {
  input: '/start',
  code: 'ERR_INVALID_URL'
}

编辑:应用修复后出现的新错误 ECONNRESET 错误

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\dis
t\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,

控制台还输出了 2 个 json 对象,称为 request 和 config,不适合这篇文章。

我注意到您的代码中有两处错误:

首先,检查您的 url 是否正确,而不是

''http://localhost:3000/start''(您有多个单引号将 url 括起来)

尝试,

"http://localhost:3000/start" 或 'http://localhost:3000/start'(用正确的双引号或单引号引起来)

其次,您将 api 调用中的数据作为请求正文而不是请求参数传递,但您正试图在 api 的参数中访问它。

您应该尝试在服务器端访问请求的主体而不是参数,

app.post('/start', async (req, res) => {
  listOfNames = req.body.listOfNames;
  res.status(200).send("Names added");
});

否则,您在访问 api 中的数据时也可能会遇到问题。 编辑:应用修复后出现的新错误 ECONNRESET 错误

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\dis
t\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,

控制台还输出了 2 个 json 对象,称为 request 和 config,不适合这篇文章。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM