Like Nic suggested, take a look at Express/lib/router code. Also, take a look at connect:
But put most simply I can make it: your router.js module will register an array of valid {URL/HTTP method/handler function} sets, and when the Node's built-in 'http' module calls it for each request, it will match url and method to execute the appropriate handler.
But put simply, they all take req.pathname that the Node.js builtin 'http' module will provide for each request. It's the HTTP path sent by the browser.
There are two phases there - setup and later, usage. In the app setup, the app starts registering routes, you can think of it as a simple array for a simple server:
[
{
path: '/profile',
method: 'GET',
handler: [Function]
}, {
path: '/notes',
method: 'GET',
handler: [Function]
}, {
path: '/notes',
method: 'POST',
handler: [Function]
}, {
path: '/',
method: 'GET',
handler: [Function]
}]
So when the app is set up, it now serves requests. Node's 'http' module will get each request (and that requests' response object, on which you can attach your reply) and give it to your router.
Your router will compare that request with the array. If it matches one, it will call it's handler, passing the request and response.
Does any of this make sense to you?
Kind of. At least I think that's the way it works, there are smarter people then me here who should do the teaching :)