从节点HTTP请求中删除端口号至外部API
问题描述:
我正在使用Node,Express和Webpack编写React应用程序。我的问题是我的API调用的URL总是在主机和路径之间有一个端口号。似乎关于此主题的大多数问题都与路由有关,而不是外部API调用。从节点HTTP请求中删除端口号至外部API
我对Request更加舒服,但是我非常沮丧地试图让它与Webpack很好地搭配,所以我转向了Node,我不太了解的http。
这里是负责API调用的方法:
getData() {
const self = this;
const url = "api.civicapps.org";
const options = {
"method": "GET",
"mode": "no-cors",
"host": url,
"path": "/restaurant-inspections/?restaurant_name=" + this.state.nameQuery,
"port": 8080,
"headers": {
"Access-Control-Allow-Origin": "http://localhost:3000"
}
}
const p1 = new Promise(function(resolve, reject) {
resolve(
http.request(options, function(res) {
res.setEncoding('utf8');
const body = {};
//This is clearly not ideal, but all I need right now is to get a response from the API
res.on('data', function(chunk) {
body.push(chunk);
});
return body;
}).end()
)
});
p1.then(function(data) {
console.log(data);
self.setState({
name: data.body
});
});
p1.catch(function(err) {
...
});
}
所有我想要做的是一个简单的测试GET请求,这个API。一旦这个工作,我会没事的。
在此先感谢。
答
事实证明,将端口设置为80,并使用sudo运行解决了问题。
你指的是什么端口?你在请求中包含一个端口('8080')。 – WiredPrairie