admin管理员组文章数量:1245088
On my node server, the following code works
axios.get('http://localhost:8080/myPath') // works
But relative pathes don't work
axios.get('/myPath') // doesn't work
I get this error :
message:"connect ECONNREFUSED 127.0.0.1:80" port:80
How can I get the relative url work like in the browser ?
Relative path should be hitting on port 8080, not 80.
Where do I set that on my node server ?
On my node server, the following code works
axios.get('http://localhost:8080/myPath') // works
But relative pathes don't work
axios.get('/myPath') // doesn't work
I get this error :
message:"connect ECONNREFUSED 127.0.0.1:80" port:80
How can I get the relative url work like in the browser ?
Relative path should be hitting on port 8080, not 80.
Where do I set that on my node server ?
Share Improve this question edited Oct 23, 2018 at 11:16 ADyson 62k16 gold badges76 silver badges90 bronze badges asked Oct 23, 2018 at 11:08 LevLev 15.7k16 gold badges63 silver badges91 bronze badges 02 Answers
Reset to default 7Create a new instance with custom configuation. like below
var instance = axios.create({ baseURL: 'http://localhost:8080' });
instance.get('/myPath', { timeout: 5000 });
Hope, this will works Reference: https://www.npmjs./package/axios
You can't use a relative URL in this scenario because there's nothing for it to be relative to - you're executing code in a script running on the server, not in a browser. It doesn't have any concept of a "current" URL to be relative to.
You'll need to explicitly specify the full domain name and port. If this causes you a problem (e.g. because you want to deploy this to different hosts without changing the code) you'll have to inject the values into your code another way (e.g. by reading from a config file, cf. documentation here).
本文标签: javascriptrelative URL not working with axios in nodeStack Overflow
版权声明:本文标题:javascript - relative URL not working with axios in node - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740244839a2248047.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论