On Nov 22, 2012, at 22:42, themitchy <[email protected]> wrote:
> I really like Naouak's idea of requiring the server into the modules that
> declare the routes. That's also a good way to demonstrate how require works
> to your advantage here. Here's a short example of what that might look like:
>
> app.js:
>
> var express = require('express');
> var app = express();
> // ... configure express, etc...
> module.exports = app;
>
> index.js:
>
> var app = require('./app');
> require('./userRoutes');
> require('./itemRoutes');
> // ... start your app, etc...
>
> userRoutes.js:
>
> var app = require('./app'); // same instance as in index.js
> app.get('/user/:id', function(req, res) { ... });
> app.get('/user/:id/friends', function(req, res) { ...});
>
> itemRoutes.js:
>
> var app = require('./app'); // same instance as in index.js and userRoutes.js
> app.get('/item/:id', function(req, res) { ... });
> app.post('/item/:id', function(req, res) { ...});
>
> What's key here is that the first time you var app = require('./app') in
> index.js, all the code in app.js will be executed. The value of app will be
> what we put into module.exports(in app.js). You could think of modules as
> functions that get executed and return their module.exports when 'required'.
> When we called require('./app') again in the other two modules we got back
> the exact same instance. The code in app.js is never run again but
> everything that was exported still has a reference to any variables or
> objects you created in that module. I only mention this last part as a
> reminder to brush up on js closures so you don't start writing memory leaks.
Although I've been dabbling in node for over a year, this point that the code
in a file is only executed once no matter how many times you require the file,
and that you'll get back the same instance each time, is something I'd only
recently come to realize. I could have worked it out sooner if I'd thought
about it, but it never even occurred to me to formulate the question. If anyone
reading this ever writes a node book or tutorial or presentation, please make
this nifty feature very clear as early as is practical.
My problem was due in part to a node tutorial I read early on in my learning,
which as I recall went to what seemed like great lengths to require everything
only once, and then pass the reference around between other required files, for
example:
var express = require('express');
var app = express();
var routes = require('routes')(app);
That tutorial also used a database, so it got a mongoose connection, and handed
that object on to the routes and other places as well. I kept trying to emulate
this structure and finding it very confusing and tedious, especially as I kept
rearranging what type of code went into each file, something which happened
rather a lot in the beginning when I didn't have a good understanding of how
and when to modularize my code. And all the examples that do everything in a
single app.js file didn't help me understand proper code modularization either.
It even took quite awhile before I realized that I didn't have to store and
retain and pass along all the mongoose models that I received as the return
value of the mongoose.model() call; I could just call mongoose.model() again
later with the model name to get them back.
Prior coding experience in other languages also colored my thinking. I jumped
ship to node from PHP, which I imagine other weary PHP developers have done and
will do. Unlike in node, in PHP, require doesn't create a new scope, and
doesn't usually return anything; it just includes the code, as if you had
pasted it in. And in PHP, as in client-side JavaScript, it's common, if
possibly bad practice, to use a few global variables here and there. For
example in the web sites that we wrote at my last web programming job, there
would typically be a MySQL connection, which got assigned to the global
variable $db. My early learning on node told me node allows no global
variables, which reinforced my incorrect thought about needing to pass
variables around between files myself.
In my current app design, the app variable is created and only used in the
entrypoint, app.js, like most examples (or the boilerplate express creates for
you). I like your idea above to make the app an export of a module and include
it from the route files instead. It's given me much to think about; thanks.
--
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