update routing logic

This commit is contained in:
Mats van Reenen 2020-07-27 20:09:42 +02:00
parent 2cc2606967
commit 02b546a5c4
2 changed files with 114 additions and 124 deletions

196
app.js
View File

@ -12,27 +12,52 @@ const app = express()
app.set('view engine', 'ejs'); app.set('view engine', 'ejs');
app.set('views', __dirname + '/views') app.set('views', __dirname + '/views')
//TODO: make the uri routing more universal. Probebly ad the siteMap creation
// routing // routing
app.use('/:vak/static', renderStatic) app.use(renderPage)
app.use('/:vak/:page', renderPage)
app.use('/:vak', renderVak)
app.use('/', renderIndex)
app.use(render404) app.use(render404)
/** This function can be used for as callback for errors //TODO: allow static content
* function renderPage(req, res, next){
* @param {Req} req Request object from Express. let path = req.path.split('/')
* @param {Res} res Responce object from Express. let cur = siteMap
* @param {Function} next The next function from Espress.
* // search for the page in siteMap
* @returns {Function(err)} a function that renders the 404 page if err is true. for (let i = 1; i < path.length; i++) {
*/ const page = path[i];
function onError404(req, res, next){
return function(err){ if(cur.type == 'directory'){
if(err) // if page is not found render 404
render404(req, res, next) 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 /** Render static content
@ -43,102 +68,17 @@ function onError404(req, res, next){
* @param {Res} res Responce object from Express. * @param {Res} res Responce object from Express.
* @param {Function} next The next function from Espress. * @param {Function} next The next function from Espress.
*/ */
function renderStatic(req, res, next){ // function renderStatic(req, res, next){
if(typeof siteMap[req.params.vak] == 'undefined') return next(); // if(typeof siteMap[req.params.vak] == 'undefined') return next();
vak = siteMap[req.params.vak] // vak = siteMap[req.params.vak]
dir = __dirname.split('/'); dir.pop() // remove dir of this code // dir = __dirname.split('/'); dir.pop() // remove dir of this code
dir = `${dir.join('/')}/${vak.title}/notes/static${req.path}` // 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 //TODO: add a custom and/or smarter 404 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: `<h1>${vak.title}</h1>` // default index page
})
}else{
compile(vak.index, function(html){
if(typeof html != 'string'){ return next() }
res.render('main', {
siteMap,
title: "notes",
topNav: vak.pages,
vak: req.params.vak,
main: html
}, onError404(req, res, next))
})
}
}
//TODO: remove playholder and acualy create the renderIndex function
/** Render the index of the site
*
* lookup the index 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 renderIndex(req, res, next){
res.render('main', {
siteMap,
title: "notes",
topNav: siteMap,
main: "<h1>Hoi</h1>"
}, onError404(req, res, next))
}
//TODO: add a custom or smarter 404 page
/** Render the 404 page /** Render the 404 page
* *
* renders a simple 404 page. * renders a simple 404 page.
@ -149,17 +89,35 @@ function renderIndex(req, res, next){
*/ */
function render404(req, res, next){ function render404(req, res, next){
res.status(404) res.status(404)
res.render('main', { // res.render('main', {
siteMap, // siteMap,
title: "notes", // main: "<h1>404 - Page not found</h1>"
topNav: vak.pages, // }, (err) => {
vak: req.params.vak, // if(err){
main: "<h1>404 - Page not found</h1>"
}, (err) => {
if(err){
res.send("<h1>404 - Page not found</h1>") res.send("<h1>404 - Page not found</h1>")
// }
// })
} }
})
//TODO: add a custom 500 page
/** Render the 500 page
*
* renders a simple 500 page.
*
* @param {Req} req Request object from Express.
* @param {Res} res Responce object from Express.
* @param {Function} next The next function from Espress.
*/
function render500(req, res, next){
res.status(500)
// res.render('main', {
// siteMap,
// main: "<h1>500 - Internal server error</h1>"
// }, (err) => {
// if(err){
res.send("<h1>500 - Internal server error</h1>")
// }
// })
} }
// start the actial web server // start the actial web server

View File

@ -1,4 +1,36 @@
<!DOCTYPE html> <%
/* title =================== */
var title = ''
let p = path.split('/')
let cur = siteMap
// search for the page in siteMap
for (let i = 1; i < p.length; i++) {
const page = p[i];
title = page.title + ' - ' + title
if(cur.type == 'directory'){
cur = cur.subPages[page]
continue
}
// page is not a directory so it's a page!
cur = cur.subPages[page]
break
}
const page = cur
delete cur
/* sideNav =================== */
var sideNav = siteMap.subPages[p[1]]
delete p
%><!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
@ -27,12 +59,12 @@
<aside class='sidenav'> <aside class='sidenav'>
<nav> <nav>
<ul> <ul>
<%_ for(i in topNav){ _%> <%_ for(i in sideNav){ _%>
<% if((typeof page !== 'undefined' && i == page) || <% if((typeof page !== 'undefined' && i == page) ||
(typeof vak !== 'undefined' && i == vak) ){ _%> (typeof vak !== 'undefined' && i == vak) ){ _%>
<li class='current'><%= topNav[i].title %></li> <li class='current'><%= sideNav[i].title %></li>
<%_ }else{ _%> <%_ }else{ _%>
<li><a href="<%= topNav[i].url %>"><%= topNav[i].title %></a></li> <li><a href="<%= sideNav[i].url %>"><%= sideNav[i].title %></a></li>
<%_ } _%> <%_ } _%>
<% } _%> <% } _%>
</ul> </ul>