From 85968e06094659f6cc23020765e147219c6c32d9 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Thu, 30 May 2019 00:25:39 +0300 Subject: [PATCH] handle db page not found improved --- src/routes/pages.js | 8 ++++---- src/server.js | 8 +++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/routes/pages.js b/src/routes/pages.js index c06afb30..3724823c 100644 --- a/src/routes/pages.js +++ b/src/routes/pages.js @@ -2,15 +2,15 @@ const {renderPage} = require('../render/page') const {get} = require('koa-route') const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash -module.exports = function () { +module.exports = function (handleNotFound) { return get('*', async ctx => { const path = getPathWithNoTrailingSlash(ctx.path) const body = await renderPage(path) if (body) { ctx.type = 'text/html' ctx.body = body - } else { - ctx.status = 404 - } + } else { + await handleNotFound(ctx) + } }) } diff --git a/src/server.js b/src/server.js index 7826e2b9..0502b582 100644 --- a/src/server.js +++ b/src/server.js @@ -33,6 +33,12 @@ const dirPublic = path.join(__dirname, '../public') handlebars.registerHelper('raw-helper', options => options.fn()) +function notFound() { + return async ctx => { + ctx.status = 404 + } +} + export default function (config) { const $ = convert @@ -77,7 +83,7 @@ export default function (config) { server.use(documents.getDocuments()) server.use(document.getDocument()) server.use(schedule.getSchedule()) - server.use(pages()) + server.use(pages(notFound())) return createServer(server.callback()) }