In Koa, can we access the Koa Context without using the pre-bound this?
For example: this
is assigned to the Koa Context:
app.use(function *(next) {
this.url;
})
But is there something like:
app.use(function *(next, ctx) {
ctx.url;
})
Why? Say we use an arrow function, this
won't be the koa context:
app.use( function *( next ) {
console.log( this.url ); // logs: "/"
( () => console.log( this.url ) )(); // logs: "undefined"
});
I know we could do:
app.use( function *( next ) {
var ctx = this;
( () => console.log( ctx.url ) )(); // logs: "/"
});
and other forms of binding but I wanted to check, that by design, this is the only way.