So I am starting to learn NodeJS and creating a simple HTTP server as given in The Node Beginner book. I have a Router
object, which contains a routing table
, which maps pathnames to the functions to be called. This is achieved by a key value object.
Now, my Server
object has a router
member which points to the above mentioned object. (For loose coupling, have kept the Router and Server separete), and a start()
method which starts the server. Which is as given below:
Server.prototype.start = function() {
var myRouter = this.router;
http.createServer(function(req, res) {
var path = url.parse(req.url).pathname;
res.write(myRouter.route(path, null));
res.end();
}).listen(80);
};
Now I have created a myRouter
variable which points to the router
reference of the Server
object, and then in the createServer
function, performing the routing using it's route()
function. This code works. However, if I omit creating the myRouter
variable part and directly perform the routing in createServer
like this:
res.write(this.router.route(path, null));
It says this.router
is undefined. I know this has something to do with scope, as the function supplied to createServer
executes later whenever a request is received, however, I am not able to understand how creating myRouter
solves this problem. Any help will be greatly appreciated.