import { WebOnErrorOptions } from '../error'; import { WebContext, WEB_PROVIDER_CONFIG_KEY } from '../web-protocol'; import Koa = require('koa'); // https://github.com/eggjs/egg/blob/master/app/middleware/notfound.js export function useNotFound(config?: WebOnErrorOptions): Koa.Middleware { const pageUrl = config?.notFoundPage; return async function notFoundMiddleware(ctx, next) { await next(); if (ctx.status !== 404 || ctx.body) { return; } // set status first, make sure set body not set status ctx.status = 404; if (ctx.accepts('html', 'text', 'json') === 'json') { ctx.body = { message: 'Not Found' }; return; } const notFoundHtml = '

404 Not Found

'; if (pageUrl && pageUrl === ctx.path) { ctx.body = `${notFoundHtml}

${WEB_PROVIDER_CONFIG_KEY}.onError.notFoundPage(${pageUrl})
is unimplemented

`; return; } if (pageUrl) { ctx.redirect(pageUrl); return; } ctx.body = notFoundHtml; }; }