0
私はゲーム用のデータを持つデータベースを持っているWebアプリケーションで作業していますし、ゲームをクリックするとAPIにGETリクエストを行います。私はあなたのアプリケーションをクリックして1つのゲームをクリックできるようにしておき、1回のゲームごとにGETリクエストを事前に行う必要なしに、そのゲームのページにリクエストの余分な情報を埋め込むことができます(約200)が、私は次の問題が生じています:ここでNodeJS:複数のGETリクエストでエラーが発生しました
_http_outgoing.js:344
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
at ServerResponse.header (C:prototype\node_modules\express\lib\response.js:718:10)
at ServerResponse.send (C:prototype\node_modules\express\lib\response.js:163:12)
at ServerResponse.json (C:prototype\node_modules\express\lib\response.js:249:15)
at EventEmitter.<anonymous> (C: prototype\webapp.js:182:14)
at emitNone (events.js:72:20)
at EventEmitter.emit (events.js:166:7)
at IncomingMessage.<anonymous> (C:webapp.js:237:17)
at emitNone (events.js:72:20)
at IncomingMessage.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:913:12)
at nextTickCallbackWith2Args (node.js:442:9)
at process._tickCallback (node.js:356:17)
は、私が実行しようとしていますコードです:
req = https.request(options, function(res) {
var responseBody ="";
res.setEncoding("UTF-8");
//retrieve the data in chunks
res.on("data", function(chunk) {
responseBody += chunk;
});
res.on("end", function(){
//Once completed we parse the data in JSON format
sendData = JSON.parse(responseBody);
console.log(sendData);
eventEmitter.emit('got_data');
});
});
req.on("error", function(err) {
console.log(`problem with request: ${err.message}`);
});
req.end();
、ここではそのためのルーティングです:
app.get('/api/gameDetails', function(req, res) {
var device = req.query.device;
var id = req.query.id;
requestForGameDetails(id, device);
eventEmitter.on('got_data', function() {
return res.json(sendData);
});
});
私は周りを見回しましたが、問題は 'return res.json(sendData);'にあります。なぜなら、これは2度呼び出されているからです。問題は解決されています。 – KC32
getルートコードでeventEmitter.onの代わりにeventEmitter.onceを使用してみてください – vbranden
ありがとうございました! :)正式に回答として提出できますか?解決策として受け入れることができますか? – KC32