Node.js’ http.Server and http.createServer, what’s the difference?

微信扫一扫,分享到朋友圈

Node.js’ http.Server and http.createServer, what’s the difference?

What is the difference between:

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

and

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

Problem courtesy of: wulfgarpro

Solution

Based on the source code of nodejs (extract below), createServer
is just a helper method to instantiate a Server
.

Extract from line 1674 of http.js
.

exports.Server = Server;


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

So therefore the only true
difference in the two code snippets you’ve mentioned in your original question, is that you’re not using the new
keyword.

For clarity the Server
constructor is as follows (at time of post – 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);

Solution courtesy of: isNaN1247

微信扫一扫,分享到朋友圈

Node.js’ http.Server and http.createServer, what’s the difference?

Nomad’s New Base Station Goes After Apple’s Absent AirPower

上一篇

A cache invalidation bug in Linux memory management

下一篇

你也可能喜欢

Node.js’ http.Server and http.createServer, what’s the difference?

长按储存图像,分享给朋友