qwik-cms/index.js

53 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-07-27 19:24:51 +02:00
import express from 'express'
import njk from 'nunjucks'
2021-08-04 17:48:11 +02:00
import njkAppend from 'nunjucks-append'
2021-07-27 19:24:51 +02:00
import fs from 'fs'
2021-07-27 21:04:57 +02:00
2021-07-27 19:24:51 +02:00
const ConfigFile = await fs.readFileSync('./config.json')
const Config = JSON.parse(ConfigFile)
const Server = express()
Server.use('/assets', express.static(Config.assetsDir))
2021-08-04 17:48:11 +02:00
const njkEnv = njk.configure(
2021-07-27 19:24:51 +02:00
Config.contentDir,
{
2021-08-04 17:48:11 +02:00
autoescape: false,
2021-07-27 21:57:48 +02:00
watch: true,
trimBlocks: true,
lstripBlocks: true,
2021-07-27 19:24:51 +02:00
express: Server
}
)
2021-08-04 17:48:11 +02:00
njkAppend.initialise(njkEnv)
2021-07-27 19:24:51 +02:00
2021-07-27 21:04:57 +02:00
Server.get(
'*',
(req, res) => {
const context = {
serverName: Config.serverName
}
if (fs.existsSync(`./${Config.contentDir}/pages/${req.path}.njk`, context))
return res.render(`pages/${req.path}.njk`)
if (fs.existsSync(`./${Config.contentDir}/pages/${req.path}/index.njk`, context))
return res.render(`pages/${req.path}/index.njk`)
return res.status(404).render('errors/404.njk', context)
}
)
2021-07-27 19:24:51 +02:00
Server.listen(
Config.serverPort,
() => {
console.log(`Started server on ${Config.serverPort}.`)
}
)