2021-08-04 20:08:33 +02:00
|
|
|
import fs from 'fs'
|
2021-08-06 14:46:16 +02:00
|
|
|
import { mdRenderer, njkRenderer } from './siteRenderer.js'
|
2021-08-04 20:08:33 +02:00
|
|
|
|
2021-08-07 10:46:48 +02:00
|
|
|
|
|
|
|
// Handle all request and try to find a corresponding file/template.
|
2021-08-04 20:08:33 +02:00
|
|
|
export function requestHandler(req, res, Config) {
|
2021-08-07 10:46:48 +02:00
|
|
|
// Check for njk files first
|
2021-08-05 23:27:11 +02:00
|
|
|
if (fs.existsSync(`./${Config.contentDir}/pages/${req.path}.njk`))
|
|
|
|
return res.send(njkRenderer(`./${Config.contentDir}/pages/${req.path}.njk`))
|
|
|
|
if (fs.existsSync(`./${Config.contentDir}/pages/${req.path}/index.njk`))
|
|
|
|
return res.send(njkRenderer(`./${Config.contentDir}/pages/${req.path}/index.njk`))
|
2021-08-04 20:08:33 +02:00
|
|
|
|
2021-08-07 10:46:48 +02:00
|
|
|
// Secondly search for markdown
|
2021-08-06 14:46:16 +02:00
|
|
|
if (fs.existsSync(`./${Config.contentDir}/pages/${req.path}.md`))
|
|
|
|
return res.send(mdRenderer(`./${Config.contentDir}/pages/${req.path}.md`))
|
|
|
|
if (fs.existsSync(`./${Config.contentDir}/pages/${req.path}/index.md`))
|
|
|
|
return res.send(mdRenderer(`./${Config.contentDir}/pages/${req.path}/index.md`))
|
|
|
|
|
2021-08-07 10:46:48 +02:00
|
|
|
// If no matching file is found, return a 404 error.
|
2021-08-06 00:33:58 +02:00
|
|
|
return res.status(404).send(njkRenderer(`./${Config.contentDir}/errors/404.njk`))
|
2021-08-04 20:08:33 +02:00
|
|
|
}
|