I would like to ask if there is a way to stop the execution of the middleware pipeline in KOA?
In the example below, I have a middleware that would somehow validate something. How could I re-code my middleware to stop the execution when validation fails?
var koa = require('koa');
var router = require('koa-router')();
var app = koa();
app.use(router.routes());
// middleware
app.use(function *(next){
var valid = false;
if (!valid){
console.log('entered validation')
this.body = "error"
return this.body; // how to stop the execution in here
}
});
// route
router.get('/', function *(next){
yield next;
this.body = "should not enter here";
});
app.listen(3000);
I could actually change my route to this:
router.get('/', function *(next){
yield next;
if(this.body !== "error")
this.body = "should not enter here";
});
But is there a better way? Or I'm missing something?
This is just an example, in reality my middleware might put a property in the body (this.body.hasErrors = true) and the route would read from that.
Again, how could I stop the execution in my middleware, so that my routes would not be executed? In Express I think you could do a response.end (not sure about this though).
Middleware is executed in the sequence you add them to the app.
You can choose to either yield to downstream middleware or send a response early (errors etc). So the key to stoping middleware flow is to not yield.
Edit for clarity:
below I've modified your original example to make it work the way I think you are intending.