Basic CMS done.
This commit is contained in:
parent
23cf82826d
commit
2fd774ff3b
|
@ -2,5 +2,6 @@
|
|||
"contentDir": "content",
|
||||
"assetsDir": "assets",
|
||||
|
||||
"serverPort": "8789"
|
||||
"serverPort": "8789",
|
||||
"serverName": "qwik"
|
||||
}
|
16
content/errors/404.njk
Normal file
16
content/errors/404.njk
Normal 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
16
content/pages/index.njk
Normal 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
3
content/pages/test1.njk
Normal file
|
@ -0,0 +1,3 @@
|
|||
<p>
|
||||
Test 1
|
||||
</p>
|
3
content/pages/test2/index.njk
Normal file
3
content/pages/test2/index.njk
Normal file
|
@ -0,0 +1,3 @@
|
|||
<p>
|
||||
Test 2
|
||||
</p>
|
21
index.js
21
index.js
|
@ -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,
|
||||
() => {
|
||||
|
|
Loading…
Reference in New Issue
Block a user