0

Event Emitter

var events = require('events');
var eventEmitter = new events.EventEmitter();

Http Server Request

var http = require('http');
var server = http.createServer(function(req, res){})

Creating instance for new events.EventEmitter() not for http.createServer()

Manoj
  • 2,000
  • 2
  • 16
  • 21
  • Possible duplicate of [Constructor function vs Factory functions](https://stackoverflow.com/questions/8698726/constructor-function-vs-factory-functions) – Luca Rainone Feb 20 '18 at 07:46

1 Answers1

0

The http library exports the Server constructor so just like with eventEmitters, you can do:

let server = new http.Server(fn);

This, is exactly the same way that you do things with the events library.

http.createServer() is a factory function. It creates a Server object for you. It's not entirely obvious why they decided to also export a factory function, but you can use either the factory function or the constructor. If you look at the code for http.createServer(), you can see that this is all it does:

function createServer(requestListener) {
  return new Server(requestListener);
}

where Server is the same thing as http.Server. So, the http library just offers two ways of creating a server, one of which works exactly like the events library.

jfriend00
  • 683,504
  • 96
  • 985
  • 979