qwik-cms/index.js

51 lines
1.0 KiB
JavaScript
Raw Normal View History

2021-07-27 19:24:51 +02:00
import express from 'express'
import njk from 'nunjucks'
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))
njk.configure(
Config.contentDir,
{
autoescape: true,
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-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}.`)
}
)