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.

-mitch


On Thursday, November 22, 2012 6:06:04 PM UTC-8, netpoetica wrote:
>
> Thanks for the responses everyone. Def. interesting about require() 
> working from cache - I'll have to experiment with that a bit, but it does 
> make sense especially since each route file will be a unique instance, it's 
> not like you're really requiring twice. Technically, the codebase itself 
> may require the same file, but, it's only really being called in if that 
> particular route is served up, so yeah I can see how require could be used 
> very efficiently.
>
> @themitchy your response way very thorough. I like the idea of using 
> require to export a specific handler like that, that is a very clean 
> solution. I guess my concern would be what if you have10 request handlers, 
> 4 or 5of which use the same code held in another module? How would you link 
> up that system of sharing?
>
> On Thursday, November 22, 2012 11:05:37 AM UTC-5, netpoetica wrote:
>>
>> I've been working with Node for a few months now, mainly out of books and 
>> tutorials, just sort of trying to get a handle on using JavaScript on the 
>> backend. It all seems well and good, but I'm still unclear about how to 
>> properly organize code in your Node program. For example, if I'm porting a 
>> completely client-side app over to Node, and I have functions that I 
>> normally use on the client side to, let say, build out an HTML widget, but 
>> I want to be able to access them from one of my route files as well so that 
>> I can build the widget behind the scenes and serve it up from the backend - 
>> where do you put this kind of code? 
>>
>> Essentially, where do you put/how do you use model/controller 
>> functions/value objects/files across multiple views in an organized, 
>> modular manner?
>>
>> We all know we don't want to pollute the global namespace, but, there are 
>> some functions that will need to be used site-wide on both the client and 
>> server side. It seems silly to me to use a require for the same function to 
>> be stored in memory again if two separate routes will need that function in 
>> order to build their instance.
>>
>> Most of the tutorials I've seen and even some Github projects have these 
>> bloated app.js files where it seems they are storing the values globally - 
>> but even then there are instances where it's unclear whether or not 
>> specific routes (files starting with module.export = ) will have access to 
>> these values (let's say, Model-level functions and value objects).
>>
>> I hope this isn't completely silly. I've done a pretty good deal of 
>> research into this, and haven't quite found anything that really speaks to 
>> this. I'm completely open to being redirected to articles you may have 
>> found and/or Github repos where simply studying the structure would make 
>> this clearer. If it's any help, I'm mostly working with Express and using a 
>> routing structure similar to typical Express apps, and just curious how to 
>> share functions and objects across multiple views, and where to put them 
>> for proper loading.
>>
>> Thanks much, and happy thanksgiving!
>>
>

-- 
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

Reply via email to