Not a silly question at all.

In the most general terms, you want to:

   - put your code into modules
   - expose that code through module.export
   - require that module wherever you may need it
   

Specific to express you could put your entire request handler in seperate 
module:

var loginHandler = require('./loginHandler');
app.get('/login', loginHandler);


And then make sure that loginHandler.js does something like:

module.exports = function(req,res) { /* perform auth */ };


Or you could put smaller blocks of functionality into logical modules. For 
example, your db storage code could live in one place:

var itemStore = require('./itemStore');
app.post('/item/:id', function(req,res) {

var item = req.body;

var id = req.params.id;

itemStore.saveItem(id, item, function(err, result) {

if (err) return res.send(err,500);

res.send(result);

}); 

});


The itemStore module could export several functions to make your life 
easier:

module.exports.saveItem = function(id, data) { /* parse some data, save an 
item */ };
module.exports.retrieveItem = function(id) { /* retrieve item, format data, 
return data */ };


Another common and useful pattern is to have your module return classes. 
 So item store could be a class that you instantiate with config data and 
reuse.  I won't get into how to build up JavaScript classes but you could 
build something that works along these lines:

var Store = require('genericStore');

var itemStore = new Store('items');
var userStore = new Store('users');

itemStore.init(/* some config or bootstrap data */);
userStore.init(/* some config or bootstrap data */);

itemStore.find(...);
userStore.updateName(...);



These are just a few patterns that I've found useful in various situations. 
 I don't think there's any right answer so don't be afraid of making 
mistakes.  Find out what works for you and then share it back with the 
community.

-mitch

On Thursday, November 22, 2012 8:05:37 AM UTC-8, 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