I don’t think there is a “right” or “wrong” here. It is just whatever fits your workflow/team best. Lots of times it is desired to enforce a directory structure, sometimes it is not. Do whatever works best for you, and helps you and future maintainers of your application understand the code better.
I personally prefer the first method, since I don’t want to edit my app.js file everytime I add a route. I want it to automagically start registering when I drop the file in, similar to many MVC frameworks. -Chad From: [email protected] [mailto:[email protected]] On Behalf Of Louis-Philippe Laurin Sent: Friday, March 07, 2014 10:42 AM To: [email protected] Subject: [nodejs] NodeJS Application Design Hi all, I am pretty new to JavaScript. My background is mostly C#. From my understanding, the app.js should only be an initialization module. Correct? What is the general rule of thumb or best pattern (If i can call it like that) when segregating the routes in a NodeJS application? I have seen this pattern many times and some people seems to like it. [APP.JS FILE] app.configure( /*This will automatically grabs all the routes from the mentioned routes directory*/ var files = fs.readdirSync(RouteDir); files.forEach(function (file) { var filePath = path.resolve('./', RouteDir, file); var route = require(filePath)(app); route(app); console.log("Route :" + file + " enabled"); }); ); But to be honest, I am really questioning this. Yes...it removes completely the "coding" dependencies but it creates an even more dangerous one which is the actual file structure of the directory since you have no control on it in your code. [APP.JS FILE] /********************************************** * Services Init **********************************************/ var someService = require("./services/userService.js"); /********************************************** * Routes Init **********************************************/ require('./routes/someRoute').addRoutes(app, someService); This one keeps a dependency (but at least you have control on it) and you pretty much know WHAT is injected and WHERE it is. Is it me who is just being wrong? Thanks in advance for your advices, LP -- -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected]<mailto:[email protected]> To unsubscribe from this group, send email to [email protected]<mailto:[email protected]> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]<mailto:[email protected]>. For more options, visit https://groups.google.com/d/optout. -- -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
