Check out this project for resource-oriented routing: https://github.com/visionmedia/express-resource . Looks pretty good too.
Gus On Mon, Jun 18, 2012 at 1:37 PM, cort fritz <[email protected]> wrote: > Thank you! > > In my ignorance I am undeterred from wanting to automatically load all > files. > > My top two reasons: > 1) I can't anticipate the case in which i would want to load routes in a > particular order. > 2) I think it is better encapsulated to have everything related to the > name of the route in one place (e.g. /routes/catalog.js) rather than split > between a "calling" file (e.g. either app.js or /routes/index.js) and the > route file (e.g. /routes/catalog.js). > > I am, however, concerned about the impact at server startup of using the > file system. I will load test that. > > > On Friday, June 15, 2012 9:39:18 PM UTC-7, nwhite wrote: >> >> There are plenty of ways to do this. I personally like the explicitness >> of defining each file rather then "load all files in directory". Load >> order, especially with routes can be important. >> >> app.js >> // app === express instance >> require('routes')(app); >> >> routes/index.js >> >> module.exports = function(app){ >> require('catalog')(app); >> } >> >> >> routes/catalog.js >> >> module.exports = function(app){ >> app.get('/catalog', function(req,res){ >> >> } >> >> app.get('/catalog/search', fn); >> } >> >> >> On Jun 15, 2012, at 12:07 PM, cort fritz <[email protected]> wrote: >> >> *BLUF* >> I want your opinions and examples for the best way to load routes in node >> >> *Detail* >> By default via installing express we split our information regarding >> routes into two files, ./app.js and ./routes/index.js. >> >> This seems odd and wrong for several (somewhat overlapping) reasons: >> >> 1. In app.js I name each route, and then name them again by virtue of >> having functions in ./routes/index.js. Could by DRYer. >> 2. I would like to encapsulate everything that defines my routes in >> ./routes >> 3. It seems primitive to have to tell my node server explicitly to >> load every file in the ./routes directory. I wasn't putting files with >> functions in there for my own amusement. Node, go get everything, ok? >> 4. I would like to be able to split my route definitions into >> multiple files so that I don't have to mess with grouping similar >> functions >> together in specific places in one file like ./app.js or >> ./routes/index.js. >> I should be able to drop a function for a new route into >> ./routes/shoppingCart.js and another one into ./routes/catalog.js, and so >> on without having to find the "shopping cart section" within >> ./routes/index.js. as a corollary, if I do have separated shopping cart >> and catalog route files, it seems brittle to have to name the files in >> ./app.js by first "requiring" them and then app.get or app.post declaring >> the routes. see #1 above. >> >> So, I like the solution proposed here: http://stackoverflow.** >> com/questions/5364928/node-js-**require-all-files-in-a-folder<http://stackoverflow.com/questions/5364928/node-js-require-all-files-in-a-folder> >> >> In that example tbranyen suggests using fs.readdirsync to loop through >> each file in ./routes and require each. a colleague suggested making this >> recursive to handle the subdirectories and that seems reasonable - I'll try >> it. >> >> My concerns as a node noob are several: >> >> 1. will one or more calls to fs.readdirsync or fs.readdir potentially >> mess up / slow down my node startup? >> 2. has this been solved in a better way by the node team or in some >> other framework or in a branch/fork thereof? >> 3. are there future plans for node or some other framework to solve >> this? >> >> Any advice from anyone appreciated. >> >> and then lower >> >> -- >> Job Board: http://jobs.nodejs.org/ >> Posting guidelines: https://github.com/joyent/**node/wiki/Mailing-List-** >> 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 >> nodejs+unsubscribe@**googlegroups.com<[email protected]> >> For more options, visit this group at >> http://groups.google.com/**group/nodejs?hl=en?hl=en<http://groups.google.com/group/nodejs?hl=en?hl=en> >> >> -- > 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 > -- 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
