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:
> In app.js I name each route, and then name them again by virtue of having
> functions in ./routes/index.js. Could by DRYer.
> I would like to encapsulate everything that defines my routes in ./routes
> 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?
> 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
>
> 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:
> will one or more calls to fs.readdirsync or fs.readdir potentially mess up /
> slow down my node startup?
> has this been solved in a better way by the node team or in some other
> framework or in a branch/fork thereof?
> 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
> 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