From 2cfe8a7244750a0407ee506b5ee62c6d106afd7a Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Tue, 9 Mar 2021 11:05:59 +0100 Subject: [PATCH] Gracefully handle fetching non-existing guides --- routes/main.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/routes/main.js b/routes/main.js index 642a064..8aad995 100644 --- a/routes/main.js +++ b/routes/main.js @@ -69,12 +69,21 @@ router.get('/guides', (req, res) => { router.get('/guides/:guideId', (req, res) => { let env = {}; - let rawContent = fs.readFileSync(`./content/guides/${req.params.guideId}.md`, "utf8", (err, data) => { - if (err) throw err; - return data; + fs.readFile(`./content/guides/${req.params.guideId}.md`, "utf8", (err, data) => { + if (err) { + res.render(`404`); + return + } + + const content = md.render(data, env); + res.render(`basic`, { title: `${env.title} - Keyoxide`, content: content }); }); - const content = md.render(rawContent, env); - res.render(`basic`, { title: `${env.title} - Keyoxide`, content: content }); + // let rawContent = fs.readFileSync(`./content/guides/${req.params.guideId}.md`, "utf8", (err, data) => { + // if (err) throw err; + // return data; + // }); + // const content = md.render(rawContent, env); + // res.render(`basic`, { title: `${env.title} - Keyoxide`, content: content }); }); module.exports = router;