Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use koa router to define a default

I'm using koa-router to define routing paths

var app = require('koa')();
var router = require('koa-router')();

app.use(router.get('/apples', function *(next) {
}));


app.use(router.get('/oranges', function *(next) {
}));

app.use(router.routes());
app.listen(8080);

How do I define a route to handle everything that hasn't got a defined route? i.e. in this case, all other paths apart from /apples and /oranges.

like image 747
mfc Avatar asked Oct 29 '25 08:10

mfc


1 Answers

You could add a catch-all after the routes. Unmatched requests should fall through to it.

router.get('/oranges', function *(next) {...});
router.get('/apples', function *(next) {...});
app.use(router.routes());
app.use(function*(next) {
  // handle unmatched...
});

...hope that helps.

like image 156
greim Avatar answered Nov 01 '25 12:11

greim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!