why should i declare handle[“/”] at request handling object in http server with node.js?


i have encoournted with this nice tutorial to node.js (http://nodebeginner.org/). there is some example there :

var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");

var handle = {}
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;

server.start(router.route, handle);

why do i need handle["/"] what is it good for?

he says there :

As you can see, it’s really simple to map different URLs to the same request handler: by adding a key/value pair of “/” and requestHandlers.start, we can express in a nice and clean way that not only requests to /start, but also requests to / shall be handled by the start handler.

why that solve any problem and what is the problem at all?

Problem courtesy of: 0x90


The function binded to handle[“/”] will be called when somebody visits yoursite.com, and handle[“/start”] will be called for visits to yoursite.com/start

The author is trying to build a server that will display different things based on routes (urls).

Solution courtesy of: alessioalex

View additional discussion.