以前にノードで実験をしました
次のコードはコンソールに「aaa」を出力し、ブラウザは期待どおりに応答を待ち続けます。
var http = require("http");
http.createServer(function (req, res) {
//res.writeHead(200);
console.log("aaa");
//res.end("hello");
//console.log("test111");
//res.end("bye");
}).listen(5555);
出力:
aaa
ただし、最初のres.endのコメントを解除するとすぐに、ノードは1回の要求でコンソールに「aaa」を2回書き込みます。
var http = require("http");
http.createServer(function (req, res) {
res.writeHead(200);
console.log("aaa");
res.end("hello");
//console.log("test111");
//res.end("bye");
}).listen(5555);
出力:
aaa
aaa
最後に、すべてのコメントを外すと、
var http = require("http");
http.createServer(function (req, res) {
res.writeHead(200);
console.log("aaa");
res.end("hello");
console.log("test111");
res.end("bye");
}).listen(5555);
出力:
aaa
test111
aaa
test111
console.logステートメントが単一のリクエストごとに2回実行される理由はありますか?