const express = require('express'); // A genareted db with all the pages const siteMap = require('./siteMap'); // All compilers for all page source types const compile = require('./compile'); const settings = require('./settings'); console.log("siteMap:", siteMap) //DEBUG // create webserver const app = express() app.set('view engine', 'ejs'); app.set('views', __dirname + '/views') // routing app.use(renderPage) app.use(render404) //TODO: allow static content function renderPage(req, res, next){ let path = req.path.split('/') let cur = siteMap // search for the page in siteMap for (let i = 1; i < path.length; i++) { const page = path[i]; if(cur.type == 'directory'){ // if page is not found render 404 if(typeof cur.subPages[page] == 'undefined'){ console.log(`404 - ${req.path}`) //TODO: add a logging system return render404(req, res, next) } cur = cur.subPages[page] continue } // page is not a directory so it's a page! cur = cur.subPages[page] break } let page = cur delete cur, path if(page.type == 'directory'){ page = page.index } compile(page, function(html){ if(typeof html != 'string') return render500() //TODO: add a logging system res.render('main', { siteMap, path: req.path, page: page, main: html }) }) } /** Render static content * * looks for the content in the folder static * * @param {Req} req Request object from Express. * @param {Res} res Responce object from Express. * @param {Function} next The next function from Espress. */ // function renderStatic(req, res, next){ // if(typeof siteMap[req.params.vak] == 'undefined') return next(); // vak = siteMap[req.params.vak] // dir = __dirname.split('/'); dir.pop() // remove dir of this code // dir = `${dir.join('/')}/${vak.title}/notes/static${req.path}` // res.sendFile(dir, onError404(req, res, next)) // } //TODO: add a custom and/or smarter 404 page /** Render the 404 page * * renders a simple 404 page. * * @param {Req} req Request object from Express. * @param {Res} res Responce object from Express. * @param {Function} next The next function from Espress. */ function render404(req, res, next){ res.status(404) // res.render('main', { // siteMap, // main: "