相对URL无法在节点中与axios一起使用 [英] relative URL not working with axios in node
问题描述
在我的节点服务器上,以下代码有效
On my node server, the following code works
axios.get('http://localhost:8080/myPath') // works
但是相对路径无效
axios.get('/myPath') // doesn't work
我收到此错误:
消息:连接ECONNREFUSED 127.0.0.1:80"端口:80
message:"connect ECONNREFUSED 127.0.0.1:80" port:80
如何获得相对网址的工作方式,例如在浏览器中?
How can I get the relative url work like in the browser ?
相对路径应该在8080端口而不是80端口上.
Relative path should be hitting on port 8080, not 80.
我在节点服务器上的哪里设置?
Where do I set that on my node server ?
推荐答案
使用自定义配置创建新实例.像下面一样
Create a new instance with custom configuation. like below
var instance = axios.create({baseURL:' http://localhost:8080 '});
instance.get('/myPath',{超时:5000});
instance.get('/myPath', { timeout: 5000 });
希望,这会起作用参考: https://www.npmjs.com/package/axios
Hope, this will works Reference: https://www.npmjs.com/package/axios
这篇关于相对URL无法在节点中与axios一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!