I do this:
path/app.js
path/routes/foo.js
path/routes/bar.js
In app.js:
var express = require('express');
var fs = require('fs');
var app = module.exports = express();
// configure app here...
// load routes
fs.readdir(__dirname + '/routes', function (err, files) {
files.forEach(function (fn) {
if(!/\.js$/.test(fn)) return;
require(__dirname + '/routes/' + fn);
});
});
app.listen(PORT);
In routes/*.js:
var app = require('../app');
app.get('/some_route', function (req, res) { ... });
On Tue, Jun 18, 2013 at 3:21 AM, Ben <[email protected]> wrote:
> Hey folks,
>
> I've searched the web pretty exhaustively, and it seems like there are
> multiple best practices for factoring express routes into separate files.
> I'm halfway through porting my code base over to the model described here:
> http://rycole.com/2013/01/28/organizing-nodejs-express.html
>
> When I stumbled upon this:
>
> http://stackoverflow.com/questions/10306185/nodejs-best-way-to-pass-common-variables-into-separate-modules
>
> Since i'm using AngularJS on my client i've become familiar with the
> dependency injection pattern. This second post above seems to indicate that
> this is a great best practice for Node as well.
>
> I'm leaning towards using DI for tackling my route separation but I wanted
> to check to see if there were any major reasons why this wouldnt be a good
> idea?
>
> Thanks!
> Ben
>
> --
> --
> 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/groups/opt_out.
>
>
>
--
--
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/groups/opt_out.