user730569
user730569

Reputation: 4004

Down the rabbit hole - Where does "res" originate from in express and/or nodejs?

I'm trying to understand more of the express and nodejs internals. Looking in express' response.js file, it frequently assigns several methods to res, which seems to be a prototype.

Specifically, res is declared as res = http.ServerResponse.prototype.

Ok, so what is http? http is declared as http = require('http').

So looking in express' http.js file, we see exports = module.exports = HTTPServer;

and HTTPServer seems to be this method:

function HTTPServer(middleware){
  connect.HTTPServer.call(this, []);
  this.init(middleware);
};

And this is where I get stuck. According to my logic, it would seem that ServerResponse is being called on the HTTPServer method, which of course doesn't make sense. Therefore, I must be missing something.

UPDATE:

I just realized that express creates an instance of HTTPServer:

exports.createServer = function(options){
  if ('object' == typeof options) {
    return new HTTPSServer(options, Array.prototype.slice.call(arguments, 1));
  } else {
    return new HTTPServer(Array.prototype.slice.call(arguments));
  }
};

So I'm guessing it's the case that ServerResponse is actually being called on that instance? But I still cannot locate ServerResponse...

Upvotes: 0

Views: 148

Answers (1)

Jerome WAGNER
Jerome WAGNER

Reputation: 22422

I can't see any http.js file in express source files.

According to node.js documentation on http http = require('http') will load the http module, which has a ServerResponse object.

So express code enhances the ServerResponse object with additionnal methods.

Upvotes: 2

Related Questions