"include" functionality?

37 views
Skip to first unread message

Jeffrey 'jf' Lim

unread,
Jul 26, 2010, 4:54:48 AM7/26/10
to expre...@googlegroups.com
Anybody wondered about this? I come from a sinatra background :), and
I very much like the ability to be able to arbitrarily split off
related functionality/code into different files (eg. I want to split
off the "app.get('/user/xxx', ...)" stuff into another file, say
users.js). In sinatra, you could do this with a 'require'.
Unfortunately, this doesnt seem possible with node/expressjs. Or is
there something i'm missing? Does anybody miss/want this
functionality?

-jf

--
"Every nonfree program has a lord, a master --
and if you use the program, he is your master."
    --Richard Stallman

"It's so hard to write a graphics driver that open-sourcing it would not help."
    -- Andrew Fear, Software Product Manager, NVIDIA Corporation
http://kerneltrap.org/node/7228

Sam Millman

unread,
Jul 26, 2010, 6:29:10 AM7/26/10
to expre...@googlegroups.com
you mean:

require("sys");

or:

require("./mongod/somecheeseyclassthatIwantincluded")

In turn this allows you to propogate a class. Is this what you mean or do you meant the php include where it acts as a part of the file?

so:

require("sys") would mean the code of that file is placed into the current document allowing the code to be manipulated as if it wasnt in a different file?


--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.




--
Bow Chicka Bow Wow

Sam Millman

unread,
Jul 26, 2010, 6:33:19 AM7/26/10
to expre...@googlegroups.com
require should work both ways since it just includes the page code into the current document then it is upto you to instantiate the object yourself.

Aaron Heckmann

unread,
Jul 26, 2010, 8:19:42 AM7/26/10
to expre...@googlegroups.com
I believe what you're looking for is in the blog example:


In app.js, the server instance is assigned to module.exports so that child modules have access to "app" (the server) as well. 
Now take a peek at contact.js


Here you see how to set up routes in child modules by accessing "module.parent.exports" which is the "app" in app.js. 

Kadir Pekel

unread,
Jul 26, 2010, 9:18:54 AM7/26/10
to expre...@googlegroups.com
Hi Jeffrey,

It was also discussed yesterday.

http://groups.google.com/group/express-js/browse_thread/thread/80f21aba4cb54e58#

You'd better subscribe to group if you are really interested in :)

Good luck with Express.
Kadir Pekel

vision media [ Tj Holowaychuk ]

unread,
Jul 26, 2010, 12:04:48 PM7/26/10
to expre...@googlegroups.com
The parent module reference is a bit weird I know, but it works just fine. I would love
suggestions for a clear way to do this, but you cant just simply require('./app') or similar 
because you will cause a cyclic require and segfault :) this is the best approach I have come
up with
Tj Holowaychuk
Vision Media
President & Creative Lead

Jeffrey 'jf' Lim

unread,
Jul 26, 2010, 11:14:43 PM7/26/10
to expre...@googlegroups.com
yeah thanks, guys! this is precisely what I'm looking for (although
yes :), a more elegant way would be great... But hey, it works!!!) - a
way to be able to refer to 'app' in require-d files... And Kadir, I am
subscribed. I just missed out on that thread. Thanks for pointing it
out!!!

-jf

Aaron Heckmann

unread,
Jul 27, 2010, 8:25:21 AM7/27/10
to expre...@googlegroups.com
The only other option I see is to export a method from your child module that accepts an app instance which you then use to wire up routes - and that is far less elegant.

Kadir Pekel

unread,
Jul 27, 2010, 8:47:20 AM7/27/10
to expre...@googlegroups.com
+1 for Hecmann's

My humble opinion is doing it with less magic for more understandable and acceptable structure.

thnks.
kadir.
-- http://twitter.com/kadirpekel

vision media [ Tj Holowaychuk ]

unread,
Jul 27, 2010, 10:06:51 AM7/27/10
to expre...@googlegroups.com
The example is not magic at all if your used to node modules, I would not call
it elegant but the point is nothing is imposed, any way you choose to get your
app from one module to another is fine :)

vision media [ Tj Holowaychuk ]

unread,
Jul 27, 2010, 10:07:37 AM7/27/10
to expre...@googlegroups.com
hell you might even want to module.exports = function(app){ app.get(...) }

and then require('./foobar')(app)

explicitly passing your app instance

Kadir Pekel

unread,
Jul 27, 2010, 10:32:36 AM7/27/10
to expre...@googlegroups.com
As a module, knowing about its parent is less 'modular' i think. The two of your approachs seems fine, but as a convention i'd rather prefer module to know nothing about any parent candidate and better waiting for milk from any of them :)

However, it rocks anyway, it's some sort of tastes within the freedom of javascript world.

vision media [ Tj Holowaychuk ]

unread,
Jul 27, 2010, 10:37:24 AM7/27/10
to expre...@googlegroups.com
Very true lol but I guess that is the point, modules cant couple very well in that sense. open to suggestions :D
Reply all
Reply to author
Forward
0 new messages