这可能是一个非常愚蠢的问题,但是有没有一种方法可以在不输入服务器地址的情况下进行获取?我想知道是否可以仅使用“ / init”代替“ http:// localhost:3000 / init”
try{
const result = await fetch("http://localhost:3001/init",
{
method:"GET",
headers:{
"content-type":"application/json"
}
});
response = await result.json();
}
catch(e){
console.log(e);
}
是否有一种无需输入服务器地址即可获取的方法
没有。
在node.js中,node-fetch
需要完全限定的URL。没有“默认”目标域或可替代的目标域或路径,就像浏览器版本为的浏览器网页中的内容一样fetch()
。
从node-fetch文档中:
fetch(url[, options])
url should be an absolute url, such as https://example.com/.
A path-relative URL (/file/under/root) or protocol-relative URL
(//can-be-http-or-https.com/) will result in a rejected Promise.
如果您真正要在此处解决的问题是能够编写可在不同主机上运行的代码(在本地和托管环境中运行),则可以使用主机名设置某种配置变量,然后构造您的主机名。在配置变量中使用主机名的URL。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句