ZijianHe / koa-router

Router middleware for koa.
MIT License
4.85k stars 408 forks source link

Custom error in case Not Found #487

Closed budarin closed 5 years ago

budarin commented 5 years ago

node.js version: 11.0.0

npm/yarn and version: 6.4.1

koa-router version: 7.4.0

koa version: 2.6.2

Code sample:

Expected Behavior:

Actual Behavior:

Additional steps, HTTP request details, or to reproduce the behavior or a test case:

I want to have custom error mesage in case when route is not found in router. How could I achieve this?

Thanks

samtsai15 commented 5 years ago
const Router = require('koa-router');
const router = new Router();
router.get('/exist',middleware);

// Need put last.
router.all('*', (ctx) => {
  ctx.body = 'Not found Custom';
  ctx.status = 404;
  return ctx;
});
budarin commented 5 years ago

thanks