Closed butlermatt closed 11 years ago
Just to add that idealy this would also handle any requests to package/ files as well for anything that may require them. Most notably, browser/dart.js however even the client-side libraries of this package to used by served files.
I definitely agree with this, thought I'm unsure whether this should be built in to Router, or a stream listener that works with any Stream
router.serve(new Glob('/_web/**').listen(serveDirectory('web', as: '/_web/'));
I can't think of an immediate reason to add a custom listener, but if you have one let me know. I also have a static file handler for single files:
router.serve('/').listen(serveFile('web/index.html'));
Thanks for the request, I'll add these soon!
After writing a massive comment for a reply, I realized that my situation, or request, is not for handler for serving a directory with static content.
Ultimately the request boils down to adding defaultHandler as we saw in the original v1 library. I love the ability that the router gives me in creating specific routes. But a lot of times adding a route for each static file is inconvenient at best, and impossible at worse. If I had a default route that was called if none of the other routes matched then I could check for my static files.
Currently the router's implementation is to call send404 if there is no route match. If a default handler was specified, you could call that instead. Or optionally have the default handler return a boolean. True if it handled the request, false if it didn't handle the request. If false then call send404.
Ah, this is a very good point, and an oversight on my part, as I always meant to have a property named 'default' which was a stream of unhandled requests.
I think the best API here is to only have the Router send 404s if there's no subscriber on the default stream. If there is, it's up to the listener to handle 404s correctly. There's really no way to send a signal back up a stream, which is why filters don't have a stream-based API.
Review up at: https://chromiumcodereview.appspot.com/12578002/
Looks great. That'll make it much easier and makes more sense than my original thought. I over complicate things sometimes ;)
Just pushed a change that adds defaultStream
@justinfagnani defaultStream
seems like a bit of a strange name. It confused me a bit when I found the property (especially since it isn't really documented). What default stream is it? If that's the default stream, then what other streams are there?
Wouldn't a name like unconsumedStream
or something along those lines make more sense?
Loving the library so far. Thanks for your work.
I love the library, particularly on the server-side for dealing with json requests from my client-side app. One thing that is a little more difficult though is setting up the initial serving/url matching for the app and its required resources.
For instance I might want to allow the following:
It would be nice to provide a convenient mechanism to allow someone to specify that '/' and be served, in addition to any other Url's specified. Something along the lines of
router.serveDir('web/', as: '/').listen(....);
where web/ is the local directory and the optional 'as' argument specifies how they're mapped, in this case any requests to '/some.file' automatically map to 'web/some.file' locally. This way it can be a last check, after other serve matches but checked before the 404 is issued.