0
Node、Express、およびWebpackを使用してReactアプリケーションを作成しています。私の問題は、私のAPI呼び出しのURLは常にホストとパスの間にポート番号を持っていることです。このトピックに関する質問のほとんどは、外部API呼び出しよりもルーティングと関連があるようです。ノードHTTPリクエストから外部APIへのポート番号を削除
私はRequestのほうがはるかに快適ですが、Webpackでうまく動くようにしようと非常に不満を感じています。私がやりたいすべてが、このAPIへの簡単なテストGET要求である
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) {
...
});
}
:ここ
は、APIの呼び出しを担当する方法です。それがうまくいくと、私は大丈夫です。
ありがとうございます。
どのポートを参照していますか?あなたはリクエストにポート( '8080')を含めています。 – WiredPrairie