two questions about static

431 views
Skip to first unread message

Andy

unread,
Mar 7, 2011, 10:32:37 PM3/7/11
to Express
#1
How can I put two separate static directories at two different paths?

express.use(express_lib['static'](__dirname + '/hello');
express.use(express_lib['static'](__dirname + '/goodbye'));

I would like to serve all files in my hello directory at /foo and all
files in my goodbye directory at /bar. Is that possible?


#2
How can I tell if a file has been serve as static? I am doing this for
some custom routing:

express.get(/.*/, function(req, res) {

And that catches static files, even though they are still being served
correctly with the static providing code. Can I tell if if a file has
already been handled, or prevent static files from hitting .get
and .post actions?

Laurie Harper

unread,
Mar 8, 2011, 6:50:54 AM3/8/11
to expre...@googlegroups.com
On 2011-03-07, at 10:32 PM, Andy wrote:
> #1
> How can I put two separate static directories at two different paths?
>
> express.use(express_lib['static'](__dirname + '/hello');
> express.use(express_lib['static'](__dirname + '/goodbye'));
>
> I would like to serve all files in my hello directory at /foo and all
> files in my goodbye directory at /bar. Is that possible?

You could try express.get('/foo', express_lib('static')(__dirname + '/hello'));
I don't think that worked quite right the last time I tried it, but the recent refactoring and clean-up for Connect 1.0 may have changed that.

> #2
> How can I tell if a file has been serve as static? I am doing this for
> some custom routing:
>
> express.get(/.*/, function(req, res) {
>
> And that catches static files, even though they are still being served
> correctly with the static providing code. Can I tell if if a file has
> already been handled, or prevent static files from hitting .get
> and .post actions?

Make sure that if you're installing the 'router' middleware, you install it *after* all 'static' middlewares, and that all your 'static' middlewares are added before you create any routes. If the 'static' middleware handles a request, if doesn't call next(), so provided the stack is ordered correctly you shouldn't set that behaviour.

If you already have your stack set up that way, post a link to a gist with minimal code that reproduces/demonstrates the problem.
--
Laurie Harper
http://laurie.holoweb.net/

vision media [ Tj Holowaychuk ]

unread,
Mar 8, 2011, 11:47:35 AM3/8/11
to expre...@googlegroups.com
yup you can have several static() middleware, hundreds even if you really wanted. the only way really to know if its a file or not is to play a middleware before it, stat() the path and see if its a file, if so it will be handled by the next static provider. Or with the beta you can use res.sendfile() to explicitly transfer the file, which uses the internals of connect.static().

PS. you can use static() as a property, JSLint is a piece of crap




--
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
Vision Media
President & Creative Lead
Reply all
Reply to author
Forward
0 new messages