I still hug my head around Node, but I have a very simple question. I see many node examples where people declare their routes and all their logic in one app.js file (or sometimes split them into subfiles).
My question basically is that it is better to keep all route declarations in the application or bootstrap as a common route that maps to your file structure. This may seem like a primitive question, but my goal is to understand what is most efficient in node.
I am currently creating an API handler with Restify, but I have another application that uses Express (so this question is likely to answer both questions).
In my route, I can either declare one bootstrap of the route like this:
app.all('/api/:package/:controller', function(request, response) { var controller = require( '../' + request.params.package + '/api/' + request.params.controller ); controller.index(request, response); response.end(); });
This basically takes all calls from the API and aims at the correct api controller. Alternatively, I can declare each route individually or perhaps even write a loop that goes through each of my controllers and declares them in init. So:
for (var i in packages.controllers) { app.all('api/' + package + '/' + controllers[i].name, function(request, response) { var controller = require( '../' + request.params.package + '/api/' + request.params.controller ); controller.index(request, response); } }
packages.controllers - an array of all possible controllers. Please note that the above code is not accurate, I have an HMVC folder structure, so the code is a little more complicated than the above. But you understand.
I wonder what the consequences of both are, and if it really matters?
Thanks!
pilotguy
source share