add notes
This commit is contained in:
parent
62119c8742
commit
7b6806753d
75
app.js
75
app.js
@ -1,28 +1,48 @@
|
|||||||
const express = require('express')
|
const express = require('express');
|
||||||
const siteMap = require('./siteMap')
|
// A genareted db with all the pages
|
||||||
|
const siteMap = require('./siteMap');
|
||||||
|
// All compilers for all page source types
|
||||||
const compile = require('./compile');
|
const compile = require('./compile');
|
||||||
const settings = require('./settings');
|
const settings = require('./settings');
|
||||||
const { render } = require('ejs');
|
|
||||||
|
|
||||||
console.log(siteMap)
|
console.log("siteMap:", siteMap) //DEBUG
|
||||||
|
|
||||||
|
// create webserver
|
||||||
const app = express()
|
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
|
||||||
app.use('/:vak/static', renderStatic)
|
app.use('/:vak/static', renderStatic)
|
||||||
app.use('/:vak/:page', renderPage)
|
app.use('/:vak/:page', renderPage)
|
||||||
app.use('/:vak', renderVak)
|
app.use('/:vak', renderVak)
|
||||||
app.use('/', renderIndex)
|
app.use('/', renderIndex)
|
||||||
app.use(render404)
|
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){
|
function onError404(req, res, next){
|
||||||
return function(err){
|
return function(err){
|
||||||
render404(req, res, next)
|
if(err)
|
||||||
|
render404(req, res, next)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 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){
|
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]
|
||||||
@ -33,6 +53,14 @@ function renderStatic(req, res, next){
|
|||||||
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){
|
function renderPage(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]
|
||||||
@ -55,17 +83,26 @@ function renderPage(req, res, next){
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 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){
|
function renderVak(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]
|
||||||
|
|
||||||
if(vak.index == null){
|
if(vak.index == null){ // no index page for this topic
|
||||||
res.render('main', {
|
res.render('main', {
|
||||||
siteMap,
|
siteMap,
|
||||||
title: "notes",
|
title: "notes",
|
||||||
topNav: vak.pages,
|
topNav: vak.pages,
|
||||||
vak: req.params.vak,
|
vak: req.params.vak,
|
||||||
main: `<h1>${vak.title}</h1>`
|
main: `<h1>${vak.title}</h1>` // default index page
|
||||||
})
|
})
|
||||||
}else{
|
}else{
|
||||||
compile(vak.index, function(html){
|
compile(vak.index, function(html){
|
||||||
@ -82,6 +119,16 @@ function renderVak(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){
|
function renderIndex(req, res, next){
|
||||||
res.render('main', {
|
res.render('main', {
|
||||||
siteMap,
|
siteMap,
|
||||||
@ -91,6 +138,15 @@ function renderIndex(req, res, next){
|
|||||||
}, onError404(req, res, next))
|
}, onError404(req, res, next))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: add a custom 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){
|
function render404(req, res, next){
|
||||||
res.status(404)
|
res.status(404)
|
||||||
res.render('main', {
|
res.render('main', {
|
||||||
@ -106,4 +162,5 @@ function render404(req, res, next){
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
app.listen(settings.serverPort, ()=>{console.log("server listening on", settings.serverPort)})
|
// start the actial web server
|
||||||
|
app.listen(settings.serverPort, ()=>{console.log("server listening on port", settings.serverPort)})
|
||||||
|
|||||||
@ -56,13 +56,13 @@ function scanDirectory(file, path){
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
title: file,
|
title: String (file),
|
||||||
type: "directory",
|
type: "directory",
|
||||||
file: path,
|
file: String (path),
|
||||||
index: (index) ? index : {
|
index: (index) ? index : {
|
||||||
title: file,
|
title: String (file),
|
||||||
type: 'index',
|
type: 'index',
|
||||||
file: path
|
file: String (path)
|
||||||
},
|
},
|
||||||
subPages: scanPages(path, files)
|
subPages: scanPages(path, files)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user