diff --git a/app.js b/app.js index c7b1fb1..827d510 100644 --- a/app.js +++ b/app.js @@ -12,27 +12,52 @@ const app = express() app.set('view engine', 'ejs'); app.set('views', __dirname + '/views') -//TODO: make the uri routing more universal. Probebly ad the siteMap creation // routing -app.use('/:vak/static', renderStatic) -app.use('/:vak/:page', renderPage) -app.use('/:vak', renderVak) -app.use('/', renderIndex) +app.use(renderPage) app.use(render404) -/** This function can be used for as callback for errors - * - * @param {Req} req Request object from Express. - * @param {Res} res Responce object from Express. - * @param {Function} next The next function from Espress. - * - * @returns {Function(err)} a function that renders the 404 page if err is true. - */ -function onError404(req, res, next){ - return function(err){ - if(err) - render404(req, res, next) +//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 @@ -43,102 +68,17 @@ function onError404(req, res, next){ * @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] +// 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}` +// 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)) -} +// res.sendFile(dir, onError404(req, res, next)) +// } -/** Render a normal page - * - * lookup the page in siteMap db and renders the page - * - * @param {Req} req Request object from Express. - * @param {Res} res Responce object from Express. - * @param {Function} next The next function from Espress. - */ -function renderPage(req, res, next){ - if(typeof siteMap[req.params.vak] == 'undefined') return next(); - vak = siteMap[req.params.vak] - - if(typeof vak.pages[req.params.page] == 'undefined') return next(); - page = vak.pages[req.params.page] - - - compile(page, function(html){ - if(typeof html != 'string'){ return next() } - - res.render('main', { - siteMap, - title: "notes", - topNav: vak.pages, - vak: req.params.vak, - page: req.params.page, - main: html - }) - }) -} - -/** Render the index of a topic - * - * lookup the topic in siteMap db and renders the index. - * if the index is not set it set the topic name as headers on the page. - * - * @param {Req} req Request object from Express. - * @param {Res} res Responce object from Express. - * @param {Function} next The next function from Espress. - */ -function renderVak(req, res, next){ - if(typeof siteMap[req.params.vak] == 'undefined') return next(); - vak = siteMap[req.params.vak] - - if(vak.index == null){ // no index page for this topic - res.render('main', { - siteMap, - title: "notes", - topNav: vak.pages, - vak: req.params.vak, - main: `