+ {{siteTitle}} is a digital give a book, take a book website for e-books. +
+
+ Books that can be diven and are available here are in the following formats:
+ {{allowedFormats}}
+ with {{maxFileSize}} maximum file size.
+
diff --git a/package.json b/package.json index b18cd67..3c42c64 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "snarkdown": "^1.2.2", "socket.io": "^2.2.0", "socket.io-client": "^2.2.0", + "striptags": "^3.1.1", "unused-filename": "^1.0.0" } } diff --git a/server.js b/server.js index 7c80b64..93a63f2 100644 --- a/server.js +++ b/server.js @@ -8,6 +8,7 @@ const bodyParser = require('body-parser'); const fileUpload = require('express-fileupload'); const filenamify = require('filenamify'); const unusedFilename = require('unused-filename'); +const striptags = require('striptags'); const snarkdown = require('snarkdown'); const fecha = require('fecha'); @@ -45,7 +46,7 @@ function Server () { this.server.use('/js', express.static(path.resolve('./node_modules/socket.io-client/dist/'))); this.server.get('/', (req, res) => { - const html = this.generateHomePage(); + const html = this.generateHomePage(req); if (html) { res.send(html); } else { @@ -54,11 +55,13 @@ function Server () { }); this.server.get('/give', (req, res) => { - const body = this.fillTemplate('./templates/pages/uploadForm.html'); - const html = this.fillTemplate('./templates/htmlContainer.html', { title: 'Give a Book', body }); + const resourcePath = (req.url.substr(-1) === '/' ? '../' : './'); + const body = this.fillTemplate('./templates/pages/uploadForm.html', { resourcePath }); + const html = this.fillTemplate('./templates/htmlContainer.html', { title: 'Give a Book', resourcePath, body }); res.send(html); }); this.server.post('/give', (req, res) => { + const resourcePath = (req.url.substr(-1) === '/' ? '../' : './'); if (Object.keys(req.files).length > 0 && req.body.hasOwnProperty('title') && req.body.title.trim() !== '' && req.body.hasOwnProperty('summary') && req.body.summary.trim() !== '') { @@ -76,7 +79,7 @@ function Server () { content: messageBox, }); const body = this.fillTemplate('./templates/pages/uploadForm.html'); - const html = this.fillTemplate('./templates/htmlContainer.html', { title: 'Give a Book', body, modal }); + const html = this.fillTemplate('./templates/htmlContainer.html', { title: 'Give a Book', resourcePath, body, modal }); res.send(html); }, (err) => { const messageBox = this.fillTemplate('./templates/elements/messageBox.html', { @@ -89,7 +92,7 @@ function Server () { content: messageBox, }); const body = this.fillTemplate('./templates/pages/uploadForm.html'); - const html = this.fillTemplate('./templates/htmlContainer.html', { title: 'Give a Book', body, modal }); + const html = this.fillTemplate('./templates/htmlContainer.html', { title: 'Give a Book', resourcePath, body, modal }); res.send(html); }); } else { @@ -109,13 +112,23 @@ function Server () { message: errorMessage, }); const body = this.fillTemplate('./templates/pages/uploadForm.html'); - const html = this.fillTemplate('./templates/htmlContainer.html', { title: 'Give a Book', body, message }); + const html = this.fillTemplate('./templates/htmlContainer.html', { title: 'Give a Book', resourcePath, body, message }); res.send(html); } }); this.server.get('/history', (req, res) => { - const html = this.generateHistoryPage(); + const html = this.generateHistoryPage(req); + if (html) { + res.send(html); + } else { + res.send('Something went wrong!'); + } + }); + + this.server.get('/about', (req, res) => { + const body = this.fillTemplate('./templates/pages/about.html'); + const html = this.fillTemplate('./templates/htmlContainer.html', { title: 'About', body }); if (html) { res.send(html); } else { @@ -173,7 +186,7 @@ Server.prototype.fillTemplate = function (file, templateVars = {}) { return data; } -Server.prototype.generateHomePage = function () { +Server.prototype.generateHomePage = function (req) { const files = fs.readdirSync(this.fileLocation).filter(fileName => fileName.includes('.json')); const books = files.map(fileName => { const bookData = JSON.parse(fs.readFileSync(path.resolve(this.fileLocation, fileName), 'utf8')); @@ -181,13 +194,13 @@ Server.prototype.generateHomePage = function () { const id = fileName.replace('.json', ''); const confirmId = 'confirm_' + id; - const added = fecha.format(new Date(bookData.added), 'dddd MMMM Do, YYYY'); + const added = fecha.format(new Date(bookData.added), 'hh:mm:ssA on dddd MMMM Do, YYYY'); const modal = this.fillTemplate('./templates/elements/modalCard.html', { id, header: '
+ {{siteTitle}} is a digital give a book, take a book website for e-books. +
+
+ Books that can be diven and are available here are in the following formats:
+ {{allowedFormats}}
+ with {{maxFileSize}} maximum file size.
+