简体   繁体   English

使用axios将请求从服务器发送到同一台服务器

[英]Sending request from server to same server with axios

I need to send request from server to same server with axios as PUT method exmp.我需要将请求从服务器发送到使用 axios 作为 PUT 方法 exmp 的同一台服务器。

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

the problem is that I am not getting any error and request is not sent.问题是我没有收到任何错误并且没有发送请求。 After while I receive Timeout error过了一会儿我收到超时错误

You should check and ensure $url, requestType and ...req.headers are correct and that you can curl that endpoint.您应该检查并确保 $url、requestType 和 ...req.headers 是正确的,并且您可以卷曲该端点。 And why not ask your question with their correct values instead of using variables whose values we don't know.为什么不用正确的值来问你的问题,而不是使用我们不知道值的变量。 In any case, here's something working in 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))

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

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