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

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

问题描述

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

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\n');
  }).listen(3500);

然后用http://xxx.xx.xxx.xx:35010击中服务器,我在控制台上看到两次request ended -我不确定为什么单个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天全站免登陆