Basic CMS done.

master
BurnyLlama 2021-07-27 21:04:57 +02:00
parent 23cf82826d
commit 2fd774ff3b
6 changed files with 58 additions and 4 deletions

View File

@ -2,5 +2,6 @@
"contentDir": "content",
"assetsDir": "assets",
"serverPort": "8789"
"serverPort": "8789",
"serverName": "qwik"
}

16
content/errors/404.njk Normal file
View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ serverName }} - Error 404</title>
</head>
<body>
<h1>Error 404</h1>
<p>
The page you are looking for doesn't exist. <br>
Consider going to the <a href="/">main page</a>. <br>
<i>{{ serverName }}</i>
</p>
</body>
</html>

16
content/pages/index.njk Normal file
View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<p>
Welcome to {{ serverName }}! <br>
<a href="/test1">Test 1</a> <br>
<a href="/test2">Test 2</a> <br>
</p>
</body>
</html>

3
content/pages/test1.njk Normal file
View File

@ -0,0 +1,3 @@
<p>
Test 1
</p>

View File

@ -0,0 +1,3 @@
<p>
Test 2
</p>

View File

@ -3,6 +3,7 @@ import njk from 'nunjucks'
import fs from 'fs'
const ConfigFile = await fs.readFileSync('./config.json')
const Config = JSON.parse(ConfigFile)
@ -20,11 +21,25 @@ njk.configure(
}
)
Server.get('*', (req, res) => {
res.send(req.url)
})
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,
() => {