node.js - nodejs - http.createServer 似乎调用了两次

标签 node.js

如果我在 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 访问服务器不知道为什么单个 HTTP 请求会导致它执行两次。

最佳答案

这很正常 - 您的浏览器会调用多个电话。

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

尝试记录网址:

console.log(req.url);

你会看到被调用的内容。

关于node.js - nodejs - http.createServer 似乎调用了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11961902/

相关文章:

node.js - NodeJS : outputting a string using Express EJS Template

javascript - 文件夹作为模块,文件夹内作为模块 NodeJS

javascript - 导出 Babel ES6 默认语法

node.js - 错误处理中间件并不总是有效

node.js - Node 打包依赖

javascript - 在 NodeJS 中将大 XLSX(超过 600MB)解析/转换为 CSV

node.js - 配置Docker服务以构建多个镜像

node.js - 在nodejs中是否有相当于 import * from 'module' 的东西?

node.js - passport-saml 和 SAML 加密

node.js - WebSocket TLS 证书设置的乐趣