But who passes the value to response in the callback function?
If you're asking who supplies the response parameter to your function, the answer (in plainest terms) is Node.js.
If you care to understand it at a deeper code level, you can examine the source. He's the definition for http.createServer():
exports.createServer = function(requestListener) {
return new Server(requestListener);
};
As can be seen, the requestListener argument is the callback you're referring to. This is used to instantiated a new Server instance.
The Server constructor is defined in the same file, and as can be seen, that requestListener gets registered as an event listener for the 'request' event:
if (requestListener) {
this.addListener('request', requestListener);
}
From there the code requires a deeper dive to understand everything that happens when a new request comes in, but at a minimum, we can see that the server eventually emits the request event with the req and res parameters:
self.emit('request', req, res);
And since your createServer() callback has been registered as an event listener for request events, it receives those parameters.
IS this passed by createServer by default in node.js?
As explained above, yes - this is default behavior.
Can I change the order of request and response?
Hopefully this is obvious from all the above, but for the sake of completeness, "no".
Edit: to address some of your general confusion on callbacks, maybe this will help...
First of all, whether we're talking about "callbacks", "event listeners", or any other common convention in evented programming, it's important to understand that at the end of the day, they're all just functions. The only thing that makes it a "callback" is a certain set of conventions about how and when it gets, well, called.
Side note: In this particular case, it would actually be more accurate to refer to the createServer() argument as an "event listener", since it gets invoked in response to a particular event (requests).
Either way, though, there's nothing magically different about it. Somewhere along the way, someone has to call that function, and the caller is going to supply the arguments that it receives. There's nothing you can do as the definer of that callback/event-listener/whatever to change the order in which those arguments are supplied, which means we simply need to rely on convention and/or documentation.