diff --git a/config.json b/config.json index 732ae9d..2ef460a 100644 --- a/config.json +++ b/config.json @@ -2,5 +2,6 @@ "contentDir": "content", "assetsDir": "assets", - "serverPort": "8789" + "serverPort": "8789", + "serverName": "qwik" } \ No newline at end of file diff --git a/content/errors/404.njk b/content/errors/404.njk new file mode 100644 index 0000000..72498fe --- /dev/null +++ b/content/errors/404.njk @@ -0,0 +1,16 @@ + + + + + + {{ serverName }} - Error 404 + + +

Error 404

+

+ The page you are looking for doesn't exist.
+ Consider going to the main page.
+ {{ serverName }} +

+ + \ No newline at end of file diff --git a/content/pages/index.njk b/content/pages/index.njk new file mode 100644 index 0000000..24d9ddd --- /dev/null +++ b/content/pages/index.njk @@ -0,0 +1,16 @@ + + + + + + Hello World! + + +

Hello World!

+

+ Welcome to {{ serverName }}!
+ Test 1
+ Test 2
+

+ + \ No newline at end of file diff --git a/content/pages/test1.njk b/content/pages/test1.njk new file mode 100644 index 0000000..aaf3a3c --- /dev/null +++ b/content/pages/test1.njk @@ -0,0 +1,3 @@ +

+ Test 1 +

\ No newline at end of file diff --git a/content/pages/test2/index.njk b/content/pages/test2/index.njk new file mode 100644 index 0000000..e671a37 --- /dev/null +++ b/content/pages/test2/index.njk @@ -0,0 +1,3 @@ +

+ Test 2 +

\ No newline at end of file diff --git a/index.js b/index.js index dd803bf..361fc9f 100644 --- a/index.js +++ b/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, () => {