nodejs - http.createServer 似乎调用了两次 [英] nodejs - http.createServer seems to call twice

查看:35
本文介绍了nodejs - http.createServer 似乎调用了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I write the following program in 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
');
  }).listen(3500);

然后用 http://xxx.xx.xxx.xx:35010 访问服务器,我在控制台上看到两次 请求结束 -- 我是不知道为什么单个 HTTP 请求会导致执行两次.

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.

推荐答案

这很正常 - 您的浏览器会拨打不止一个电话.

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

例如,大多数浏览器都会调用获取/favicon.ico.

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

尝试记录网址:

console.log(req.url);

然后你会看到被调用的内容.

and you'll see what's being called.

这篇关于nodejs - http.createServer 似乎调用了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆