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
