mirror of
https://github.com/koajs/examples.git
synced 2025-12-08 19:24:58 +00:00
27 lines
569 B
JavaScript
27 lines
569 B
JavaScript
const Koa = require('koa');
|
|
|
|
const app = module.exports = new Koa();
|
|
|
|
app.use(async function pageNotFound(ctx) {
|
|
// we need to explicitly set 404 here
|
|
// so that koa doesn't assign 200 on body=
|
|
ctx.status = 404;
|
|
|
|
switch (ctx.accepts('html', 'json')) {
|
|
case 'html':
|
|
ctx.type = 'html';
|
|
ctx.body = '<p>Page Not Found</p>';
|
|
break;
|
|
case 'json':
|
|
ctx.body = {
|
|
message: 'Page Not Found'
|
|
};
|
|
break;
|
|
default:
|
|
ctx.type = 'text';
|
|
ctx.body = 'Page Not Found';
|
|
}
|
|
});
|
|
|
|
if (!module.parent) app.listen(3000);
|