nodejs - http。createServer似乎打了两次电话。

时间:2022-07-16 01:14:46

If I write the following program in node:

如果我在node编写以下程序:

  http.createServer(function (req, res) {

    if( req.method == 'GET' ) {
      var body = ''; req.on('data', function(data) { body += data });
      req.on('end',  function() {
        console.log('request ended')
      });
    }

    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('142\n');
  }).listen(3500);

And then hit the server with http://xxx.xx.xxx.xx:35010 I see a request ended twice on my console -- I'm not sure why a single HTTP request is causing this to execute twice.

然后使用http://xxx.xx.xxx访问服务器。xx:35010我看到一个请求在我的控制台中结束了两次——我不确定为什么一个HTTP请求会导致这个请求执行两次。

1 个解决方案

#1


116  

That is normal - your browser makes more than one call.

这很正常——你的浏览器会发出不止一个调用。

Most browsers make a call to grab /favicon.ico for example.

大多数浏览器都会调用抓取/图标。图标为例。

Try to log the url:

尝试记录url:

console.log(req.url);

and you'll see what's being called.

你会看到它的名字。

#1


116  

That is normal - your browser makes more than one call.

这很正常——你的浏览器会发出不止一个调用。

Most browsers make a call to grab /favicon.ico for example.

大多数浏览器都会调用抓取/图标。图标为例。

Try to log the url:

尝试记录url:

console.log(req.url);

and you'll see what's being called.

你会看到它的名字。