A
A
abusabir2015-10-24 17:11:45
JavaScript
abusabir, 2015-10-24 17:11:45

Question about koa. What do we have next?

I had no experience with Express, maybe that's why the simple moment is incomprehensible.
In a simple koa application example, we pass a generator with the next parameter, which is then yielded inside the application. What is it for?

app.use(function* (next) {
    if (this.request.path === '/') {
        this.response.body = 'hello world';
    } else {
        yield next;
    }
});

update: Seems to be clearing up. All the strangeness is connected with the fact that in such an example this is not so necessary, and if there are several such middlewares, they will transfer control to each other. Correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aves, 2015-10-24
@abusabir

Yes, right. The middleware must either end the request or call the next one.

app.use(function* (next) {
    if (this.request.path === '/') {
        this.response.body = 'hello world';
    } else {
        //yield next;
    }
});
app.use(function* (next) {
    this.response.body = 'hello world not from root';
    yield next;
});

Without next, only access to / will work, with next - to any address.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question