Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why yield next #9

Closed
ForbesLindesay opened this issue Dec 17, 2013 · 9 comments
Closed

Why yield next #9

ForbesLindesay opened this issue Dec 17, 2013 · 9 comments

Comments

@ForbesLindesay
Copy link

I'm trying to get to grips with how koa is built. What is the purpose of yield next: index.js#L36

Am I right in thinking that static files are only served if the request is not handled by any other method? That seems really backwards compared to express's nice waterfalling semantics. It's simple to read an express app as "try this then try that then try that then try that" and it just stops once a method succeeds in handling the request. How would something like the following work:

var serve = require('static');
var koa = require('koa');
var app = koa();

app.use(serve('/first'));
app.use(serve('/second'));

app.listen(3000);

console.log('listening on port 3000');
@tj
Copy link
Member

tj commented Dec 17, 2013

that's correct, in this one we're currently letting it go all the way downstream and then determining if something else handled it. That's one of the gotchas with "proper" middleware, but I think both cases are valid, I'd like to support both see #8

@jonathanong
Copy link
Member

i think this is the right approach when your static files change during production or when you're developing. i wouldn't want a stat call on every single request made on my production server.

that's why i wrote https://github.com/koajs/static-cache for production, which doesn't use a single stat call except during initialization.

@tj
Copy link
Member

tj commented Dec 18, 2013

in production you'd either use something like varnish or a cdn (that's why I wouldn't bother with it in node), but there are some exceptions, and you can set up nginx to route certain paths in prod, but maintain the same behaviour in node

@jonathanong
Copy link
Member

yeah, static-cache is an over optimization. i'm averse to infrastructure though, especially for simple stuff. people are still going to GET crap like /apple-icon-precomposed-XXxXX.png in every single size, and you can't really avoid that without infrastructure.

@ForbesLindesay
Copy link
Author

I can see why you'd want to put your static middleware last for certain applications, but not why you wouldn't just respect the user's choice - i.e. recommend putting static last rather than first.

The yield pattern makes a lot of sense for things like logging/gzipping middleware where it needs to hook in something when the request starts and do some kind of additional work when the request ends. I just don't see how it makes sense for middleware that just serves files.

@ForbesLindesay
Copy link
Author

Thanks for confirming what it does though. That has helped me understand :)

@tj
Copy link
Member

tj commented Dec 19, 2013

I'll doc this better in the readme too, it's a little confusing when you're coming from Connect since it looks the same. I do think supporting both is good though, especially when mounting; stat() first makes sense

@ForbesLindesay
Copy link
Author

Perhaps make it default to connect's behavior, which is intuitively what most people would expect, and then have a postpone option (or named something else) to enable this behavior when people want it. That way people reading the code will see that the middleware has been explicitly postponed.

@tj
Copy link
Member

tj commented Dec 19, 2013

agreed! { defer: true } or something, sounds good to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants