51 lines
1.0 KiB
JavaScript
51 lines
1.0 KiB
JavaScript
import express from 'express'
|
|
import njk from 'nunjucks'
|
|
import fs from 'fs'
|
|
|
|
|
|
|
|
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,
|
|
watch: true,
|
|
trimBlocks: true,
|
|
lstripBlocks: true,
|
|
express: Server
|
|
}
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
}
|
|
)
|
|
|
|
Server.listen(
|
|
Config.serverPort,
|
|
() => {
|
|
console.log(`Started server on ${Config.serverPort}.`)
|
|
}
|
|
) |