qwik-cms/index.js

60 lines
1.6 KiB
JavaScript
Raw Normal View History

import fs from 'fs'
2021-07-27 19:24:51 +02:00
import express from 'express'
2021-07-27 19:24:51 +02:00
import njk from 'nunjucks'
2021-08-04 20:08:33 +02:00
import njkMarkdown from 'nunjucks-markdown'
import marked from 'marked'
2021-07-27 19:24:51 +02:00
import { requestHandler } from './libs/requestHandler.js'
import { markedRenderer } from './libs/markedRenderer.js'
2021-07-27 19:24:51 +02:00
2021-07-27 21:04:57 +02:00
// Load in config
2021-08-04 20:08:33 +02:00
const ConfigFile = fs.readFileSync('./config.json')
2021-07-27 19:24:51 +02:00
const Config = JSON.parse(ConfigFile)
2021-08-07 10:46:48 +02:00
// Create a server object
2021-07-27 19:24:51 +02:00
const Server = express()
2021-08-07 10:46:48 +02:00
// Configure the assets directory
2021-07-27 19:24:51 +02:00
Server.use('/assets', express.static(Config.assetsDir))
2021-08-07 10:46:48 +02:00
// Configure the nunjucks environment (HTML templating)
2021-08-04 17:48:11 +02:00
const njkEnv = njk.configure(
2021-07-27 19:24:51 +02:00
Config.contentDir,
{
2021-08-07 10:46:48 +02:00
autoescape: true, // Automatically escape HTML aka a '<' will become '&lt;'
watch: true, // Automatically reload all templates if they change on disk.
trimBlocks: true, // Remove trailing newlines from tags
lstripBlocks: true, // Automatically remove leading whitespace from tags
express: Server // Use the express server. (Currently obsolete, I think.)
2021-07-27 19:24:51 +02:00
}
)
2021-08-07 10:46:48 +02:00
// Configure marked (markdown parser)
2021-08-07 10:16:13 +02:00
marked.use({
2021-08-07 10:46:48 +02:00
gfm: true, // Use GitHub formatting?
renderer: markedRenderer // Use my own custom rendering for som tags
2021-08-04 22:52:49 +02:00
})
2021-08-07 10:46:48 +02:00
// Let nunjucks use markdown.
2021-08-04 20:08:33 +02:00
njkMarkdown.register(njkEnv, marked)
2021-07-27 19:24:51 +02:00
2021-07-27 21:04:57 +02:00
// Send all requests to the requestHandler.
Server.get('*', (req, res) => requestHandler(req, res, Config))
// Start the server
2021-07-27 19:24:51 +02:00
Server.listen(
Config.serverPort,
() => {
console.log(`Started server on ${Config.serverPort}.`)
}
)