One thing you may be missing is that requiring a module multiple times does not load multiple version of it. Modules are cached after the first time they are required, so you get the same reference every time. So one answer to your question is that people re-use modules by requiring them in multiple contexts. You might require('helper') in multiple views or controllers and it's not a problem. You might also check out geddyjs http://geddyjs.org/, which takes a rails-like approach in providing conventions of where to place your code.
As for sharing modules between client and server, I agree there isn't a shared convention for this yet. I personally haven't found compelling reasons to share a bunch of code between environments yet. :Marco 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en