express.static :: namespacing static folders with URL's

1,268 views
Skip to first unread message

Phil Cockfield

unread,
Jul 3, 2011, 12:33:55 AM7/3/11
to Express
CONTEXT:

With [express.static] doing something like this:

app.use(express.static(__dirname + '/public'));

Will allow the [public] folder to server static assets from the URL
starting at /

----

QUESTION:

But say I have another static resources folder I want to open up,
which is organized somewhere else within the project (for instance,
within a module within the node_modules folder).

Say I wanted to have resources within this other static folder come
off URL's starting at:

/foo/

How would I do that? When I use [express.static] multiple times, I
see that those folders all start at the root of the URL.

I guess I could namespace by putting everything within a named folder
within the actual folder I'm passing to [express.static], but is there
a more elegant way to do this - like pass a URL argument to app.use?

Thanks guys!

Josh Chaney

unread,
Jul 3, 2011, 5:43:06 AM7/3/11
to expre...@googlegroups.com
Try a softlink. 

If you had:
app.use(express.static(__dirname + '/public'));

in /public you would do:
ln -s /lib/some_module/assets foo

-- 
Josh Chaney

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

TJ Holowaychuk

unread,
Jul 3, 2011, 11:50:32 AM7/3/11
to expre...@googlegroups.com
app.use('/foo', express.static(__dirname + '/public'));

GET /foo/javascripts/jquery.js

etc

Connect has this concept of "mounting" built in, where the middleware is routed based on it, however does not see the prefix given, so static() treats this request as "/javascripts/jquery.js"

-- 
TJ Holowaychuk

On Saturday, July 2, 2011 at 9:33 PM, Phil Cockfield wrote:

Phil Cockfield

unread,
Jul 3, 2011, 1:22:03 PM7/3/11
to expre...@googlegroups.com
Beautiful!  Figured there's be a way to do this nicely.

Your API design on Express is deeply elegant Tj.

Phil Cockfield

unread,
Jul 3, 2011, 1:22:57 PM7/3/11
to expre...@googlegroups.com
Beautiful!  Figured there'd be a way to do this nicely.

Your API design on Express is deeply elegant Tj.
On Sun, Jul 3, 2011 at 8:50 AM, TJ Holowaychuk <t...@vision-media.ca> wrote:

TJ Holowaychuk

unread,
Jul 3, 2011, 1:23:24 PM7/3/11
to expre...@googlegroups.com
thanks man! appreciate it. It will be getting a bit better for 3.x :D need to start working on that soon

-- 
TJ Holowaychuk

alessio_alex

unread,
Jul 4, 2011, 11:53:18 AM7/4/11
to Express
Thanks TJ, didn't know this could be done.

Perhaps it's better to put this info on the official guide too.
> > > > For more options, visit this group athttp://groups.google.com/group/express-js?hl=en.
>
> > >  --
> > >  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 athttp://groups.google.com/group/express-js?hl=en.

Dale Tan

unread,
Jul 4, 2011, 2:21:47 PM7/4/11
to expre...@googlegroups.com, Express
Correct me if I'm wrong, but I think should be able to fork express, then add stuff to the "docs" folder, and then send a pull request to tj with your updates.

--- dale

TJ Holowaychuk

unread,
Jul 4, 2011, 5:37:28 PM7/4/11
to expre...@googlegroups.com
yup. i typically update the site each release but anything in the markdown will be in there. we almost need a section devoted to tips/tricks etc, but I guess the examples serve for that

-- 
TJ Holowaychuk
Reply all
Reply to author
Forward
0 new messages