Where are the `req` and `res` coming from?

NOTE: This question has very little jQuery, Drupal, or node.js it's more of a generic question on "how frameworks achieve X, where X is something any of the frameworks I mentioned also provides.

I saw an example node.js code that looks like this:

var http = require('http');
var server = http.createServer();
server.listen(8000);
server.on('request', function(req, res) {
    //do something with req and res here
});

There is no obvious place where req and res are coming from. In fact, what does 'request' mean? Where is it supplied from?

I have noticed similar things in jQuery .get() and .post() functions, and looking at the source did not help as much as I would like. I've even seen this being done in Drupal; a function is defined in the theme layer or as a module_hook with specific naming conventions by me, but arguments appear outta nowhere and there is a predictable structure of data (specified in the manual) inside those magic variables.

So what is this technique called, and how does it work. I've heard tell of Dependency Injection... is this it? If it is, could you explain in n00b terms how it is accomplished?

This is particularly confusing because I coded in procedural from the start, and we always know where a variable is coming from and how a function is being called...

The framework constructs the objects for you, and passes them to your callback.

N.B. req and res are just parameter names; you could call them spam and eggs, or hocus and pocus, for all it matters.

In fact, what does request mean? Where is it supplied from?

Whenever you want to access a web site, you're using a special protocol, the hypertext transfer protocol (HTTP). This protocol mainly uses two things:

  1. a question from the client like "what is / on your server?" (the request)
  2. an answer from the server like "it's a text/html, the length is 2000 bytes, and here is the document" (the response).

This request-response model is used directly in node.js, as the server you're using is a HTTP server.

[...] could you explain in n00b terms how it is accomplished?

Do you know what a main-loop or event-loop is? Almost every GUI application has one. It's basically a loop like this:

while(waitForNewEvent(&event)){
    handleMsg(&event);
}

This event can be anything, from keyboard input to another software trying to bring your window to front. It can also be something like "are you ready for standby?".

node.js uses such a event-loop in it's server implementation. server.on('request', callback) basically tells node.js that you want callback to be used whenever a request is coming:

while(waitForNewEvent(&event)){
    if(event == "request"){
        callback(request,&response);
        responseToClient(response);
    }
}

Intern example

Or even simplier: think of a intern, who's just running around in circles in a building. He's the event-loop. Now in your server room someone tells him that every request should be brought to them. He writes this down and continues on his never-ending tour.

Then someone stands in front of the building and wants to check his bank-account. He simply throws a request into a post box and the intern rushes to the server room and tells the technicians that the specific site has been requested and gives them the necessary information. However, he needs to wait on their response, since their response isn't on his list.

The technicians check the request and find out that the user isn't qualified for the given request(*). They prepare an error message and give it to the intern. He now returns to the front of the building, gives the error message to the first client and is ready for other messages.

(*): At this point they might need to check something in a database, which might take some time. They could tell the intern to come back later and call him if they're ready. In this case the intern could continue his way until the technicians are ready.

You're passing the function to the .on() function. When the event occurs, some internal code invokes the function you passed, and provides the arguments to it.

Here's an example. The server object has a method named on. It takes a name string and a callback function.

It uses setTimeout to wait one second before invoking the callback it was given. When it invokes it, it passes to it the name that was provided, as well as a static message "hi there".

  // Think of this as the internal Node code...
var server = {         // v---this will be the function you pass
    on: function(name, callback) {
            setTimeout(function() {
                callback(name, "hi there"); // here your function is invoked
            }, 1000);
        }
};

So here we call .on(), and pass it the name "foo", and the callback function. When the callback is invoked, it will be given the name, and the "hi there" message.

   // ...and this is your code.
server.on("foo", function(name, message) {
    console.log(name, message);
});

They are short for "Request" and "Response." It is typical of many web frameworks to pass these two objects into a request handling method (action or whatever you want to call it).