微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – Node.js的http.Server和http.createServer有什么区别?

有什么区别:

http.Server(function(req,res) {});

http.createServer(function(req,res) {});

解决方法

根据nodejs的源代码(下面的解析),createServer只是一个帮助程序来实例化一个服务器.

摘自line 1674 of http.js.

exports.Server = Server;


exports.createServer = function(requestListener) {
  return new Server(requestListener);
};

因此,您在原始问题中提到的两个代码段中唯一真正的区别是,您没有使用新的关键字.

为了清楚起见,Server构造函数如下(在2012-12-13之后):

function Server(requestListener) {
  if (!(this instanceof Server)) return new Server(requestListener);
  net.Server.call(this,{ allowHalfOpen: true });

  if (requestListener) {
    this.addListener('request',requestListener);
  }

  // Similar option to this. Too lazy to write my own docs.
  // http://www.squid-cache.org/Doc/config/half_closed_clients/
  // http://wiki.squid-cache.org/SquidFaq/InnerWorkings#What_is_a_half-closed_filedescriptor.3F
  this.httpAllowHalfOpen = false;

  this.addListener('connection',connectionListener);

  this.addListener('clientError',function(err,conn) {
    conn.destroy(err);
  });
}
util.inherits(Server,net.Server);

原文地址:https://www.jb51.cc/js/154313.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐