qwik-cms/index.js
2021-08-07 10:46:48 +02:00

60 lines
1.6 KiB
JavaScript

import fs from 'fs'
import express from 'express'
import njk from 'nunjucks'
import njkMarkdown from 'nunjucks-markdown'
import marked from 'marked'
import { requestHandler } from './libs/requestHandler.js'
import { markedRenderer } from './libs/markedRenderer.js'
// Load in config
const ConfigFile = fs.readFileSync('./config.json')
const Config = JSON.parse(ConfigFile)
// Create a server object
const Server = express()
// Configure the assets directory
Server.use('/assets', express.static(Config.assetsDir))
// Configure the nunjucks environment (HTML templating)
const njkEnv = njk.configure(
Config.contentDir,
{
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.)
}
)
// Configure marked (markdown parser)
marked.use({
gfm: true, // Use GitHub formatting?
renderer: markedRenderer // Use my own custom rendering for som tags
})
// Let nunjucks use markdown.
njkMarkdown.register(njkEnv, marked)
// Send all requests to the requestHandler.
Server.get('*', (req, res) => requestHandler(req, res, Config))
// Start the server
Server.listen(
Config.serverPort,
() => {
console.log(`Started server on ${Config.serverPort}.`)
}
)