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.