【问题标题】:Sending request from server to same server with axios使用 axios 从服务器发送请求到同一台服务器
【发布时间】:2021-07-26 08:10:46
【问题描述】:

我需要使用 axios 作为 PUT 方法示例将请求从服务器发送到同一台服务器。

await axios({
        url: `http://localhost:4000${url}`,
        method: requestType,
        headers: { ...req.headers, 'Content-Type': 'multipart/form-data' },
        data,
 }).catch((err) => console.log('error :>> ', err))

问题是我没有收到任何错误并且没有发送请求。之后我收到超时错误

【问题讨论】:

    标签: javascript node.js express axios


    【解决方案1】:

    您应该检查并确保 $url、requestType 和 ...req.headers 是正确的,并且您可以卷曲该端点。为什么不使用正确的值来问您的问题,而不是使用我们不知道其值的变量。无论如何,这里有一些在 express 中工作的东西

    const axios = require('axios')
    const express = require('express')
    const app = express()
    const port = 4000
    
    app.get('/', (req, res) => {
     res.send('Hello World!')
    })
    
    const url = `http://localhost:${port}`;
    
    app.listen(port, () => {
     console.log(`Example app listening at ${url}`)
    })
    
    axios({
     url: url,
     method: 'get', // tried with post and put too
    })
    .then(res => {
     console.log(res.data)
    })
    .catch((err) => console.log('error :>> ', err))
    

    【讨论】:

    • 问题出在标题中,我发送了多个不正确的标题。谢谢
    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 2019-04-19
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多