Nodejs Http Server api Поток выполнения, пожалуйста, объясните? - PullRequest
0 голосов
/ 10 декабря 2018

У меня установлена ​​следующая программа Nodejs

var http = require('http');
var url = require('url');
var server = http.createServer((req,res)=>{
  console.log("Enter into the 3000 port");
  res.end("hello world")
  console.log("Enter into the 3000 port");
}).listen(3000,()=>{console.log("the server is listen to the port 3000");});

Я выполняю этот код при загрузке localhost: 3000 в браузере, и когда я написал console.log("Enter into the 3000 port");, чтобы проверить, как выполняется внутреннее выполнение, я получилследующий вывод.ВЫХОД:

the server is listen to the port 3000
Enter into the 3000 port
Enter into the 3000 port
Enter into the 3000 port
Enter into the 3000 port

Но я написал код console.log("Enter into the 3000 port"); два раза в коде, но я не понимаю, почему он вызывался два раза по одному запросу, и когда я снова отправляю запрос, он снова показал мнекакой-то вывод может объяснить любой.

Ответы [ 2 ]

0 голосов
/ 10 декабря 2018
var http = require('http');
var url = require('url');
var server = http.createServer((req,res)=>{
  if (req.url === '/favicon.ico') { return } // if you don't serve this hit 
  console.log(req.url);
  console.log("Enter into the 3000 port");
  res.end("hello world")
  console.log("Enter into the 3000 port");
}).listen(3000,()=>{console.log("the server is listen to the port 3000");})

enter image description here

большинство браузеров ищут *favicon.ico* автоматически, которого вы можете избежать, если хотите

код

0 голосов
/ 10 декабря 2018

Это нормально - ваш браузер делает более одного вызова.

Большинство браузеров, например, делают вызов для вызова /favicon.ico.

Попробуйте ввести URL:

console.log(req.url);

и вы увидите, что называется.

...