From a937a97f412b6dbd109c5cbae4634f160b9e0c31 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 2 Mar 2019 17:07:05 -0500 Subject: [PATCH 01/11] Created files for the routes used in this project. --- package.json | 1 + routes/about.js | 14 +++++ routes/admin/analytics.js | 8 +++ routes/admin/downloads.js | 8 +++ routes/admin/index.js | 35 ++++++++++++ routes/admin/login.js | 8 +++ routes/admin/posts.js | 8 +++ routes/admin/users.js | 8 +++ routes/category/index.js | 8 +++ routes/contact.js | 13 +++++ routes/downloads/index.js | 9 +++ routes/includes/index.js | 9 +++ routes/index.js | 24 ++++++++ routes/projects/index.js | 8 +++ server.js | 115 +++++++++++++++++++------------------- 15 files changed, 218 insertions(+), 58 deletions(-) create mode 100644 routes/about.js create mode 100644 routes/admin/analytics.js create mode 100644 routes/admin/downloads.js create mode 100644 routes/admin/index.js create mode 100644 routes/admin/login.js create mode 100644 routes/admin/posts.js create mode 100644 routes/admin/users.js create mode 100644 routes/category/index.js create mode 100644 routes/contact.js create mode 100644 routes/downloads/index.js create mode 100644 routes/includes/index.js create mode 100644 routes/index.js create mode 100644 routes/projects/index.js diff --git a/package.json b/package.json index 802a1d6..32f89e7 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "promise": "^8.0.1", "remarkable": "^1.7.1", "request": "^2.88.0", + "routes": "^2.1.0", "sanitizer": "^0.1.3", "sendmail": "^1.4.1" }, diff --git a/routes/about.js b/routes/about.js new file mode 100644 index 0000000..9d95db2 --- /dev/null +++ b/routes/about.js @@ -0,0 +1,14 @@ +const routes = require('express').Router(); + +routes.post('/', (request, result) => +{ + +}); + + +routes.get('*', (request, result) => +{ + //error +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/admin/analytics.js b/routes/admin/analytics.js new file mode 100644 index 0000000..973d8ec --- /dev/null +++ b/routes/admin/analytics.js @@ -0,0 +1,8 @@ +const routes = require('express').Router(); + +routes.post('/', (request, result) => +{ + +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/admin/downloads.js b/routes/admin/downloads.js new file mode 100644 index 0000000..973d8ec --- /dev/null +++ b/routes/admin/downloads.js @@ -0,0 +1,8 @@ +const routes = require('express').Router(); + +routes.post('/', (request, result) => +{ + +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/admin/index.js b/routes/admin/index.js new file mode 100644 index 0000000..cdb51cc --- /dev/null +++ b/routes/admin/index.js @@ -0,0 +1,35 @@ +const routes = require('express').Router(); + +const analytics = require('./analytics'); +routes.use('/analytics', analytics); + +const login = require('./login'); +routes.use('/login', login); + +const posts = require('./posts'); +routes.use('/posts', posts); + +const downloads = require('./downloads'); +routes.use('/downloads', downloads); + +const users = require('./users'); +routes.use('/users', users); + +routes.get('/', (request, result) => +{ + if(utils.checkPrivilege(request) >= utils.PRIVILEGE.MEMBER) + { + utils.renderHTML(request, result, "users.html", getUserInformation); + } + else + { + utils.printError(result, "You need to be logged in"); + } +}); + +routes.get('*', (request, result) => +{ + //error +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/admin/login.js b/routes/admin/login.js new file mode 100644 index 0000000..973d8ec --- /dev/null +++ b/routes/admin/login.js @@ -0,0 +1,8 @@ +const routes = require('express').Router(); + +routes.post('/', (request, result) => +{ + +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/admin/posts.js b/routes/admin/posts.js new file mode 100644 index 0000000..973d8ec --- /dev/null +++ b/routes/admin/posts.js @@ -0,0 +1,8 @@ +const routes = require('express').Router(); + +routes.post('/', (request, result) => +{ + +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/admin/users.js b/routes/admin/users.js new file mode 100644 index 0000000..973d8ec --- /dev/null +++ b/routes/admin/users.js @@ -0,0 +1,8 @@ +const routes = require('express').Router(); + +routes.post('/', (request, result) => +{ + +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/category/index.js b/routes/category/index.js new file mode 100644 index 0000000..973d8ec --- /dev/null +++ b/routes/category/index.js @@ -0,0 +1,8 @@ +const routes = require('express').Router(); + +routes.post('/', (request, result) => +{ + +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/contact.js b/routes/contact.js new file mode 100644 index 0000000..b6d1ffc --- /dev/null +++ b/routes/contact.js @@ -0,0 +1,13 @@ +const routes = require('express').Router(); + +routes.post('/', (request, result) => +{ + +}); + +routes.get('*', (request, result) => +{ + //error +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/downloads/index.js b/routes/downloads/index.js new file mode 100644 index 0000000..887044f --- /dev/null +++ b/routes/downloads/index.js @@ -0,0 +1,9 @@ +const routes = require('express').Router(); + +routes.post('/', (request, result) => +{ + +}); + + +module.exports = routes; \ No newline at end of file diff --git a/routes/includes/index.js b/routes/includes/index.js new file mode 100644 index 0000000..887044f --- /dev/null +++ b/routes/includes/index.js @@ -0,0 +1,9 @@ +const routes = require('express').Router(); + +routes.post('/', (request, result) => +{ + +}); + + +module.exports = routes; \ No newline at end of file diff --git a/routes/index.js b/routes/index.js new file mode 100644 index 0000000..695e6c4 --- /dev/null +++ b/routes/index.js @@ -0,0 +1,24 @@ +const routes = require('express').Router(); + +const about = require('./about'); +routes.use('/about', about); + +const contact = require('./contact'); +routes.use('/contact', contact); + +const projects = ["/steam/"]; + + +routes.get('/', (request, result) => +{ + //blog home +}); + + +routes.get('*', (request, result) => +{ + //error +}); + + +module.exports = routes; \ No newline at end of file diff --git a/routes/projects/index.js b/routes/projects/index.js new file mode 100644 index 0000000..973d8ec --- /dev/null +++ b/routes/projects/index.js @@ -0,0 +1,8 @@ +const routes = require('express').Router(); + +routes.post('/', (request, result) => +{ + +}); + +module.exports = routes; \ No newline at end of file diff --git a/server.js b/server.js index 9bcac45..7e8ea56 100644 --- a/server.js +++ b/server.js @@ -11,12 +11,6 @@ const config = require('./utils/configLoader').getConfig(); /** Port for the server to run on */ const port = config.PORT; -/** http server */ -const http = require('http'); - -/** used to parse the request URL */ -const url = require('url'); - /** express app */ const express = require("express"); @@ -42,62 +36,67 @@ const session = require('express-session'); app.use(session({ secret: config.SESSION_SECRET, cookie: { maxAge: 6000000 }})); -const projects = ["/steam/"]; -/** - * Parses the request url and calls correct JS files - */ -app.use(function(request, result) -{ - //prevents people from pointing their dns at my IP:port for my site - if(request.headers.host.includes("localhost:" + port) || - request.headers.host.includes("jrtechs.net")) - { - const filename = url.parse(request.url, true).pathname; - - var project = false; - projects.forEach(function(projectName) - { - if(filename.startsWith(projectName)) - { - require("./sites/projects.js").main(request, result, projectName); - project = true; - } - }); - - if(filename.startsWith("/admin")) - { - require("./sites/admin.js").main(request, result, filename); - project = true; - } - - if(!project) - { - require("./sites/blog.js").main(request, result, filename); - } - - try - { - const getClientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0] - || request.connection.remoteAddress; - console.log(getClientAddress); - - sql.logTraffic(getClientAddress, filename); - } - catch (e) - { } - } - else - { - // utils.printWrongHost(result); - result.writeHead(418, {}); - result.end(); - } -}); + +const routes = require('./routes'); +app.use('/', routes); + +// /** +// * Parses the request url and calls correct JS files +// */ +// app.use(function(request, result) +// { +// //prevents people from pointing their dns at my IP:port for my site +// if(request.headers.host.includes("localhost:" + port) || +// request.headers.host.includes("jrtechs.net")) +// { +// const filename = url.parse(request.url, true).pathname; +// +// var project = false; +// projects.forEach(function(projectName) +// { +// if(filename.startsWith(projectName)) +// { +// require("./sites/projects.js").main(request, result, projectName); +// project = true; +// } +// }); +// +// if(filename.startsWith("/admin")) +// { +// require("./sites/admin.js").main(request, result, filename); +// project = true; +// } +// +// if(!project) +// { +// require("./sites/blog.js").main(request, result, filename); +// } +// +// try +// { +// const getClientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0] +// || request.connection.remoteAddress; +// console.log(getClientAddress); +// +// sql.logTraffic(getClientAddress, filename); +// } +// catch (e) +// { } +// } +// else +// { +// // utils.printWrongHost(result); +// result.writeHead(418, {}); +// result.end(); +// } +// }); //enables gzip compression for the site app.use(compression()); -http.createServer(app).listen(port); \ No newline at end of file +app.listen(port, () => + console.log(`App listening on port ${port}!`) +); \ No newline at end of file From 857de1222bc84ea260fb2fefd9918b2bb06f42f4 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sun, 3 Mar 2019 10:51:04 -0500 Subject: [PATCH 02/11] Got the contact page to work with the new routes system. --- blog/contact.js | 1 - includes/includes.js | 69 +++++++++++++++++++++++++++++++++++---- routes/about.js | 2 +- routes/admin/analytics.js | 2 +- routes/admin/downloads.js | 2 +- routes/admin/login.js | 2 +- routes/admin/posts.js | 2 +- routes/admin/users.js | 2 +- routes/category/index.js | 2 +- routes/contact.js | 4 +-- routes/downloads/index.js | 8 +++-- routes/includes/index.js | 14 +++++++- routes/index.js | 7 +++- routes/projects/index.js | 2 +- sites/blog.js | 4 --- utils/utils.js | 39 ++-------------------- 16 files changed, 100 insertions(+), 62 deletions(-) diff --git a/blog/contact.js b/blog/contact.js index 4527720..c75703b 100644 --- a/blog/contact.js +++ b/blog/contact.js @@ -215,7 +215,6 @@ module.exports = const html = whiskers.render(content[0], templateContext); result.write(html); result.end(); - }).catch(function(err) { console.log(err); diff --git a/includes/includes.js b/includes/includes.js index 68eb7fc..4d2deb8 100644 --- a/includes/includes.js +++ b/includes/includes.js @@ -30,6 +30,25 @@ const crypto = require('crypto'); //caching program to make the application run faster const cache = require('memory-cache'); +const fs = require('fs'); + + +const readFile = function(filename) +{ + return new Promise(function(resolve, reject) + { + try + { + resolve(fs.readFileSync(filename)); + } + catch (e) + { + console.log(e); + console.log("Could not find " + filename); + return(""); + } + }) +}; /** * Sends a static file to the client in a way which the web browser @@ -46,7 +65,7 @@ const sendCachedContent = function(path, type, result) if(goods == null) { - utils.include("." + path).then(function(content) + readFile(path).then(function(content) { const eTag = crypto.createHash('md5').update(content).digest('hex'); result.writeHead(200, {'Content-Type': type, 'Cache-Control': @@ -83,10 +102,48 @@ module.exports = */ printHeader: function(templateContext) { - return utils.includeInObject(HEADER_KEY, templateContext, HEADER_FILE); + return module.exports.includeInObject(HEADER_KEY, templateContext, HEADER_FILE); }, + includeInObject: function(key, context, fileName) + { + return new Promise(function(resolve, reject) + { + readFile(fileName).then(function(result) + { + console.log(result); + context[key] = result; + resolve(); + }).catch(function(error) + { + context[key] = "File Not Found"; + reject(error); + console.log(error); + }) + }); + }, + + + /** + * A function similar to the include statement in PHP + * This function writes a file to the output + * + * @param fileName the file to append to the result + */ + include: function(fileName) + { + try + { + return(fs.readFileSync(fileName)); + } + catch (e) + { + console.log("Could not find " + fileName); + return(""); + } + }, + /** * Appends the footer to the result object * @@ -94,7 +151,7 @@ module.exports = */ printFooter: function(templateContext) { - return utils.includeInObject(FOOTER_KEY, templateContext, FOOTER_FILE); + return module.exports.includeInObject(FOOTER_KEY, templateContext, FOOTER_FILE); }, /** @@ -114,7 +171,7 @@ module.exports = * @param path * @return {*} */ - sendCSS: function(result, path, cache) + sendCSS: function(result, path) { sendCachedContent(path, 'text/css', result); }, @@ -145,7 +202,7 @@ module.exports = fetchTemplate: function(templateName) { - return utils.include("templates/" + templateName); + return readFile("templates/" + templateName); }, @@ -156,7 +213,7 @@ module.exports = */ sendHTML: function(result, fileName) { - utils.include("." + fileName).then(function(content) + readFile("." + fileName).then(function(content) { result.writeHead(200, {'Content-Type': 'text/html'}); result.write(content); diff --git a/routes/about.js b/routes/about.js index 9d95db2..d5ca964 100644 --- a/routes/about.js +++ b/routes/about.js @@ -1,6 +1,6 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +routes.get('/', (request, result) => { }); diff --git a/routes/admin/analytics.js b/routes/admin/analytics.js index 973d8ec..5c68232 100644 --- a/routes/admin/analytics.js +++ b/routes/admin/analytics.js @@ -1,6 +1,6 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +routes.get('/', (request, result) => { }); diff --git a/routes/admin/downloads.js b/routes/admin/downloads.js index 973d8ec..5c68232 100644 --- a/routes/admin/downloads.js +++ b/routes/admin/downloads.js @@ -1,6 +1,6 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +routes.get('/', (request, result) => { }); diff --git a/routes/admin/login.js b/routes/admin/login.js index 973d8ec..5c68232 100644 --- a/routes/admin/login.js +++ b/routes/admin/login.js @@ -1,6 +1,6 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +routes.get('/', (request, result) => { }); diff --git a/routes/admin/posts.js b/routes/admin/posts.js index 973d8ec..5c68232 100644 --- a/routes/admin/posts.js +++ b/routes/admin/posts.js @@ -1,6 +1,6 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +routes.get('/', (request, result) => { }); diff --git a/routes/admin/users.js b/routes/admin/users.js index 973d8ec..5c68232 100644 --- a/routes/admin/users.js +++ b/routes/admin/users.js @@ -1,6 +1,6 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +routes.get('/', (request, result) => { }); diff --git a/routes/category/index.js b/routes/category/index.js index 973d8ec..5c68232 100644 --- a/routes/category/index.js +++ b/routes/category/index.js @@ -1,6 +1,6 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +routes.get('/', (request, result) => { }); diff --git a/routes/contact.js b/routes/contact.js index b6d1ffc..4c81fae 100644 --- a/routes/contact.js +++ b/routes/contact.js @@ -1,8 +1,8 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +routes.get('/', (request, result) => { - + require("../blog/contact.js").main(request, result); }); routes.get('*', (request, result) => diff --git a/routes/downloads/index.js b/routes/downloads/index.js index 887044f..01ec4f1 100644 --- a/routes/downloads/index.js +++ b/routes/downloads/index.js @@ -1,8 +1,12 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => -{ +/** used to parse the request URL */ +const url = require('url'); +routes.get('/', (request, result) => +{ + const filename = url.parse(request.url, true).pathname; + require("../../includes/downloads.js").main(result, filename); }); diff --git a/routes/includes/index.js b/routes/includes/index.js index 887044f..70a5ddf 100644 --- a/routes/includes/index.js +++ b/routes/includes/index.js @@ -1,8 +1,20 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +/** used to parse the request URL */ +const url = require('url'); + +const contentLoader = require('../../includes/staticContentServer.js'); + + + +routes.get('*', (request, result) => { + var filename = url.parse(request.url, true).pathname; + if(!contentLoader.serveStaticContent(request, result, filename, "includes")) + { + //missing content + } }); diff --git a/routes/index.js b/routes/index.js index 695e6c4..a30cbad 100644 --- a/routes/index.js +++ b/routes/index.js @@ -6,12 +6,17 @@ routes.use('/about', about); const contact = require('./contact'); routes.use('/contact', contact); +const includes = require('./includes'); +routes.use('/includes', includes); + const projects = ["/steam/"]; + + routes.get('/', (request, result) => { - //blog home + //blog home page }); diff --git a/routes/projects/index.js b/routes/projects/index.js index 973d8ec..5c68232 100644 --- a/routes/projects/index.js +++ b/routes/projects/index.js @@ -1,6 +1,6 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +routes.get('/', (request, result) => { }); diff --git a/sites/blog.js b/sites/blog.js index 1c1b142..3d798ae 100644 --- a/sites/blog.js +++ b/sites/blog.js @@ -43,10 +43,6 @@ module.exports= { require("../includes/downloads.js").main(result, filename); } - else if (filename.includes("/contact")) - { - require("../blog/contact.js").main(request, result); - } else { var page = request.query.page; diff --git a/utils/utils.js b/utils/utils.js index 376bc89..534d7a9 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -6,47 +6,12 @@ //used for file io const fs = require('fs'); +const includes = require("../includes/includes"); + module.exports= { - /** - * A function similar to the include statement in PHP - * This function writes a file to the output - * - * @param fileName the file to append to the result - */ - include: function(fileName) - { - return new Promise(function(resolve, reject) - { - try - { - resolve(fs.readFileSync(fileName)); - } - catch (e) - { - console.log("Could not find " + fileName); - resolve(""); - } - }); - }, - includeInObject: function(key, context, fileName) - { - return new Promise(function(resolve, reject) - { - module.exports.include(fileName).then(function(result) - { - context[key] = result; - resolve(); - }).catch(function(error) - { - context[key] = "File Not Found"; - reject(error); - console.log(error); - }) - }) - }, /** From f4e0438361924616bf128175ad9f325208564e17 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sun, 3 Mar 2019 11:05:06 -0500 Subject: [PATCH 03/11] Updated the downloads route to work. --- includes/downloads.js | 17 ++++------------- includes/includes.js | 10 +--------- includes/staticContentServer.js | 1 - routes/downloads/index.js | 3 ++- routes/index.js | 5 ++++- utils/utils.js | 9 +++++++-- 6 files changed, 18 insertions(+), 27 deletions(-) diff --git a/includes/downloads.js b/includes/downloads.js index 0387160..7bf65a4 100644 --- a/includes/downloads.js +++ b/includes/downloads.js @@ -26,13 +26,10 @@ module.exports= return new Promise(function(resolve, reject) { const urlSplit = requestURL.split("/"); - console.log(urlSplit); - if(urlSplit.length == 3 || urlSplit.length == 4) + if(urlSplit.length == 2 || urlSplit.length == 4) { - console.log(urlSplit[2]); - sql.getDownload(urlSplit[2]).then(function(result) + sql.getDownload(urlSplit[1]).then(function(result) { - console.log(result); if(result.length == 1) { const file = './blogContent/downloads/' + @@ -41,19 +38,13 @@ module.exports= } else { - utils.print404(res).then(function() - { - resolve(); - }) + utils.print404(res); } }); } else { - utils.print404().then(function(content) - { - resolve(content); - }) + utils.print404(res); } }); } diff --git a/includes/includes.js b/includes/includes.js index 4d2deb8..4c9d755 100644 --- a/includes/includes.js +++ b/includes/includes.js @@ -133,15 +133,7 @@ module.exports = */ include: function(fileName) { - try - { - return(fs.readFileSync(fileName)); - } - catch (e) - { - console.log("Could not find " + fileName); - return(""); - } + return readFile(fileName); }, /** diff --git a/includes/staticContentServer.js b/includes/staticContentServer.js index bd2e417..905ddc4 100644 --- a/includes/staticContentServer.js +++ b/includes/staticContentServer.js @@ -2,7 +2,6 @@ const includes = require('../includes/includes.js'); - /** * @author Jeffery Russell 10-30-18 * diff --git a/routes/downloads/index.js b/routes/downloads/index.js index 01ec4f1..36bf405 100644 --- a/routes/downloads/index.js +++ b/routes/downloads/index.js @@ -3,8 +3,9 @@ const routes = require('express').Router(); /** used to parse the request URL */ const url = require('url'); -routes.get('/', (request, result) => +routes.get('*', (request, result) => { + console.log("ping"); const filename = url.parse(request.url, true).pathname; require("../../includes/downloads.js").main(result, filename); }); diff --git a/routes/index.js b/routes/index.js index a30cbad..faf2e42 100644 --- a/routes/index.js +++ b/routes/index.js @@ -6,6 +6,9 @@ routes.use('/about', about); const contact = require('./contact'); routes.use('/contact', contact); +const downloads = require('./downloads'); +routes.use('/downloads', downloads); + const includes = require('./includes'); routes.use('/includes', includes); @@ -22,7 +25,7 @@ routes.get('/', (request, result) => routes.get('*', (request, result) => { - //error + console.log("Un registered event.") }); diff --git a/utils/utils.js b/utils/utils.js index 534d7a9..933089a 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -111,8 +111,13 @@ module.exports= * @param result * @returns {*} */ - print404: function() + print404: function(result) { - return this.include("includes/html/404.html"); + includes.include("includes/html/404.html").then(function(html) + { + result.write(html); + result.end(); + }) + } }; \ No newline at end of file From c7ffc3a8cbc5e43280526f6a4f18b588c813b7d8 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sun, 3 Mar 2019 13:28:50 -0500 Subject: [PATCH 04/11] Updated admin routes to support the login. --- admin/adminDownloads.js | 20 ++++++++++++--- admin/adminHome.js | 24 +++++++++++++---- admin/posts.js | 18 ++++++++++++- admin/users.js | 21 ++++++++++++++- includes/includes.js | 2 +- routes/about.js | 4 ++- routes/admin/index.js | 18 ++++++------- routes/admin/login.js | 8 ++++++ routes/contact.js | 4 ++- routes/index.js | 8 ++++++ sites/admin.js | 47 +++++++++++++--------------------- templates/admin/adminMain.html | 2 +- utils/utils.js | 46 +++++++++++++++++++++++++++++++++ 13 files changed, 170 insertions(+), 52 deletions(-) diff --git a/admin/adminDownloads.js b/admin/adminDownloads.js index 08e45d6..717a43d 100644 --- a/admin/adminDownloads.js +++ b/admin/adminDownloads.js @@ -109,13 +109,11 @@ module.exports= * @param templateContext json object used as the template context * @returns {Promise} renders the template used for this page */ - main: function(postData, templateContext) + main: function(templateContext) { return new Promise(function(resolve, reject) { Promise.all([includes.fetchTemplate(TEMPLATE_FILE), - addDownloadPostData(postData), - removeDownloads(postData), displayDownloads(templateContext)]).then(function(template) { resolve(template[0]); @@ -125,5 +123,21 @@ module.exports= reject(error); }); }); + }, + + processPostData: function(postData, templateContext) + { + return new Promise(function(resolve, reject) + { + Promise.all([addDownloadPostData(postData), + removeDownloads(postData)]).then(function() + { + resolve(); + }).catch(function(error) + { + console.log(error); + reject(error); + }) + }) } }; \ No newline at end of file diff --git a/admin/adminHome.js b/admin/adminHome.js index c1dbdd0..0cc2e34 100644 --- a/admin/adminHome.js +++ b/admin/adminHome.js @@ -116,15 +116,12 @@ module.exports= * @param templateContext json object used as the template context * @returns {Promise} renders the template used for this page */ - main: function(postData, templateContext) + main: function(templateContext) { - console.log("called"); return new Promise(function(resolve, reject) { Promise.all([includes.fetchTemplate(TEMPLATE_FILE), - processPostAddCategory(postData), - appendCategoriesToTemplate(templateContext), - processPost(postData)]) + appendCategoriesToTemplate(templateContext)]) .then(function(template) { resolve(template[0]); @@ -134,5 +131,22 @@ module.exports= reject(error); }); }); + }, + + processPostData: function(postData, templateContexgt) + { + return new Promise(function(resolve, reject) + { + Promise.all([processPostAddCategory(postData), + processPost(postData)]) + .then(function(data) + { + resolve(); + }).catch(function(error) + { + console.log("error in add downloads.js"); + reject(error); + }); + }); } }; \ No newline at end of file diff --git a/admin/posts.js b/admin/posts.js index 019fb14..d5a9f68 100644 --- a/admin/posts.js +++ b/admin/posts.js @@ -84,7 +84,23 @@ module.exports= * @param templateContext json object used as the template context * @returns {Promise} renders the template used for this page */ - main: function(postData, templateContext) + main: function(templateContext) + { + return new Promise(function(resolve, reject) + { + Promise.all([includes.fetchTemplate(TEMPLATE_FILE), + fetchPostsInformation(templateContext)]).then(function(template) + { + resolve(template[0]); + }).catch(function(error) + { + console.log("error in add admin blog.js"); + reject(error); + }); + }); + }, + + processPostData(postData, templateContext) { return new Promise(function(resolve, reject) { diff --git a/admin/users.js b/admin/users.js index b0d1c4c..74fecde 100644 --- a/admin/users.js +++ b/admin/users.js @@ -167,7 +167,26 @@ module.exports= resolve(template[0]); }).catch(function(error) { - console.log("error in add downloads.js"); + console.log("error in users.js"); + reject(error); + }); + }); + }, + + processPostData: function(postData, templateContext) + { + return new Promise(function(resolve, reject) + { + Promise.all([includes.fetchTemplate(TEMPLATE_FILE), + addUserPostData(postData), + removeUserPost(postData), + editUserPost(postData, templateContext), + getUserInformation(templateContext)]).then(function(template) + { + resolve(template[0]); + }).catch(function(error) + { + console.log("error in users.js"); reject(error); }); }); diff --git a/includes/includes.js b/includes/includes.js index 4c9d755..d7365de 100644 --- a/includes/includes.js +++ b/includes/includes.js @@ -153,7 +153,7 @@ module.exports = */ printAdminHeader(templateContext) { - return utils.includeInObject(HEADER_KEY, templateContext, ADMIN_HEADER); + return module.exports.includeInObject(HEADER_KEY, templateContext, ADMIN_HEADER); }, diff --git a/routes/about.js b/routes/about.js index d5ca964..ff2818c 100644 --- a/routes/about.js +++ b/routes/about.js @@ -1,5 +1,7 @@ const routes = require('express').Router(); +const utils = require('../utils/utils.js'); + routes.get('/', (request, result) => { @@ -8,7 +10,7 @@ routes.get('/', (request, result) => routes.get('*', (request, result) => { - //error + utils.print404(result); }); module.exports = routes; \ No newline at end of file diff --git a/routes/admin/index.js b/routes/admin/index.js index cdb51cc..fad942d 100644 --- a/routes/admin/index.js +++ b/routes/admin/index.js @@ -15,21 +15,21 @@ routes.use('/downloads', downloads); const users = require('./users'); routes.use('/users', users); +const utils = require('../../utils/utils.js'); + routes.get('/', (request, result) => { - if(utils.checkPrivilege(request) >= utils.PRIVILEGE.MEMBER) - { - utils.renderHTML(request, result, "users.html", getUserInformation); - } - else - { - utils.printError(result, "You need to be logged in"); - } + utils.constructAdminPage(request, result, require("../../admin/adminHome").main) +}); + +routes.post('/', (request, result) => +{ + utils.constructAdminPage(request, result, require("../../admin/adminHome").processPostData); }); routes.get('*', (request, result) => { - //error + utils.print404(result); }); module.exports = routes; \ No newline at end of file diff --git a/routes/admin/login.js b/routes/admin/login.js index 5c68232..22c208f 100644 --- a/routes/admin/login.js +++ b/routes/admin/login.js @@ -1,8 +1,16 @@ const routes = require('express').Router(); +const utils = require('../../utils/utils.js'); + routes.get('/', (request, result) => { + utils.constructAdminPage(request, result, require("../../admin/adminHome").main) +}); + +routes.post('/', (request, result) => +{ + utils.constructAdminPage(request, result, require("../../admin/login").main) }); module.exports = routes; \ No newline at end of file diff --git a/routes/contact.js b/routes/contact.js index 4c81fae..bce9656 100644 --- a/routes/contact.js +++ b/routes/contact.js @@ -1,5 +1,7 @@ const routes = require('express').Router(); +const utils = require('../utils/utils.js'); + routes.get('/', (request, result) => { require("../blog/contact.js").main(request, result); @@ -7,7 +9,7 @@ routes.get('/', (request, result) => routes.get('*', (request, result) => { - //error + utils.print404(result); }); module.exports = routes; \ No newline at end of file diff --git a/routes/index.js b/routes/index.js index faf2e42..74e0fae 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,14 +1,22 @@ const routes = require('express').Router(); +/** about page */ const about = require('./about'); routes.use('/about', about); +/** admin page and all of its sub pages */ +const admin = require('./admin'); +routes.use('/admin', admin); + +/** Contact page */ const contact = require('./contact'); routes.use('/contact', contact); +/** Downloads path for files */ const downloads = require('./downloads'); routes.use('/downloads', downloads); +/** static content like css, js, and images */ const includes = require('./includes'); routes.use('/includes', includes); diff --git a/sites/admin.js b/sites/admin.js index bbfa674..9b9ea2a 100644 --- a/sites/admin.js +++ b/sites/admin.js @@ -1,9 +1,6 @@ //sending static content const includes = require('../includes/includes.js'); -//used to append static content to result -const contentLoader = require('../includes/staticContentServer.js'); - const whiskers = require('whiskers'); @@ -22,34 +19,26 @@ module.exports= */ main: function(request, result, filename) { - if(contentLoader.serveStaticContent(request, result, filename, "")) - { - //do nothing - } - else - { - const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0] - || request.connection.remoteAddress; + const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0] + || request.connection.remoteAddress; - result.writeHead(200, {'Content-Type': 'text/html'}); + result.writeHead(200, {'Content-Type': 'text/html'}); - const file = "../admin/admin.js"; + const file = "../admin/admin.js"; - var templateContext = Object(); - Promise.all([includes.fetchTemplate("admin/adminMain.html"), - includes.printAdminHeader(templateContext), - require(file).main(request, clientAddress, templateContext, filename), - includes.printFooter(templateContext), - ]).then(function(content) - { - result.write(whiskers.render(content[0], templateContext)); - result.end(); - - }).catch(function(err) - { - console.log(err); - throw err; - }); - } + var templateContext = Object(); + Promise.all([includes.fetchTemplate("admin/adminMain.html"), + includes.printAdminHeader(templateContext), + require(file).main(request, clientAddress, templateContext, filename), + includes.printFooter(templateContext), + ]).then(function(content) + { + result.write(whiskers.render(content[0], templateContext)); + result.end(); + }).catch(function(err) + { + console.log(err); + throw err; + }); } }; \ No newline at end of file diff --git a/templates/admin/adminMain.html b/templates/admin/adminMain.html index 94bbd8f..82cf0d6 100644 --- a/templates/admin/adminMain.html +++ b/templates/admin/adminMain.html @@ -26,7 +26,7 @@

Login

-
+
diff --git a/utils/utils.js b/utils/utils.js index 933089a..b43cbbf 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -6,6 +6,9 @@ //used for file io const fs = require('fs'); +const whiskers = require('whiskers'); + + const includes = require("../includes/includes"); @@ -118,6 +121,49 @@ module.exports= result.write(html); result.end(); }) + }, + + + constructAdminPage: function(request, result, templateFiller) + { + var templateContext = Object(); + var promises = []; + + promises.push(includes.fetchTemplate("admin/adminMain.html")); + promises.push(includes.printAdminHeader(templateContext)); + promises.push(includes.printFooter(templateContext)); + + if(module.exports.loggedIn(request)) + { + promises.push(templateFiller(templateContext)); + } + else + { + //login + const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0] + || request.connection.remoteAddress; + promises.push(require("../admin/login").main(request, clientAddress,templateContext)); + } + Promise.all(promises).then(function(content) + { + result.write(whiskers.render(content[0], templateContext)); + result.end(); + }).catch(function(err) + { + console.log(err); + throw err; + }); + }, + + adminPostRoute: function(request, result, templateFiller) + { + + }, + + + loggedIn(request) + { + return(request.session && request.session.user); } }; \ No newline at end of file From c86145125c99c5511463c9331bbc86f2756110e3 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Mon, 4 Mar 2019 21:08:25 -0500 Subject: [PATCH 05/11] Updated route to get admin home and posts pages to work. --- admin/adminHome.js | 35 ++++++++++++++++++----------- admin/posts.js | 15 ++++++++----- routes/admin/index.js | 5 ++++- routes/admin/login.js | 1 + routes/admin/posts.js | 7 ++++++ utils/utils.js | 51 ++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 94 insertions(+), 20 deletions(-) diff --git a/admin/adminHome.js b/admin/adminHome.js index 0cc2e34..42f07ab 100644 --- a/admin/adminHome.js +++ b/admin/adminHome.js @@ -29,13 +29,19 @@ const processPostAddCategory = function(postData) if(sql.insert(q) != 0) { console.log("category added"); + resolve(); } else { + reject(); console.log("error adding category"); } } - resolve(""); + else + { + resolve(); + } + }); }; @@ -74,7 +80,6 @@ const processPost = function(postData) urls = urls.split(" ").join("-"); urls =urls.toLowerCase(); - var q = "insert into posts (category_id, picture_url, published, name, url) values "; q += "('" + post.add_post_category + "', '" + post.add_post_picture + @@ -83,7 +88,7 @@ const processPost = function(postData) { var map = require('../utils/generateSiteMap'); map.main(); - resolve(""); + resolve(); }).catch(function(error) { reject(error); @@ -93,15 +98,17 @@ const processPost = function(postData) { require("../sites/blog.js").clearCache(); require("../includes/includes.js").clearCache(); + resolve(); } else if(post.git_pull) { const execSync = require('child_process').execSync; - code = execSync('git pull') + code = execSync('git pull'); + resolve(); } else { - resolve(""); + resolve(); } }); }; @@ -124,29 +131,31 @@ module.exports= appendCategoriesToTemplate(templateContext)]) .then(function(template) { - resolve(template[0]); + templateContext.adminPage = template[0]; + resolve(); }).catch(function(error) { - console.log("error in add downloads.js"); + console.log("error in add adminHome.js"); reject(error); }); }); }, - processPostData: function(postData, templateContexgt) + processPostData: function(postData) { return new Promise(function(resolve, reject) { Promise.all([processPostAddCategory(postData), processPost(postData)]) - .then(function(data) + .then(function() { + console.log("all resolved"); resolve(); }).catch(function(error) - { - console.log("error in add downloads.js"); - reject(error); - }); + { + console.log("error in add downloads.js"); + reject(error); + }); }); } }; \ No newline at end of file diff --git a/admin/posts.js b/admin/posts.js index d5a9f68..6871bef 100644 --- a/admin/posts.js +++ b/admin/posts.js @@ -10,6 +10,9 @@ const sql = require('../utils/sql'); const qs = require('querystring'); +const utils = require('../utils/utils'); + + /** * Detects if the post data came from the edit form in blog table or edit post * in the edit post form. @@ -91,16 +94,17 @@ module.exports= Promise.all([includes.fetchTemplate(TEMPLATE_FILE), fetchPostsInformation(templateContext)]).then(function(template) { - resolve(template[0]); + templateContext.adminPage = template[0]; + resolve(); }).catch(function(error) { - console.log("error in add admin blog.js"); + console.log("error in add admin posts.js"); reject(error); }); }); }, - processPostData(postData, templateContext) + processPostData(templateContext, postData) { return new Promise(function(resolve, reject) { @@ -108,10 +112,11 @@ module.exports= processPostData(postData, templateContext), fetchPostsInformation(templateContext)]).then(function(template) { - resolve(template[0]); + templateContext.adminPage = template[0]; + resolve(); }).catch(function(error) { - console.log("error in add admin blog.js"); + console.log("error in add admin posts.js"); reject(error); }); }); diff --git a/routes/admin/index.js b/routes/admin/index.js index fad942d..c0e2768 100644 --- a/routes/admin/index.js +++ b/routes/admin/index.js @@ -24,7 +24,10 @@ routes.get('/', (request, result) => routes.post('/', (request, result) => { - utils.constructAdminPage(request, result, require("../../admin/adminHome").processPostData); + utils.adminPostRoute(request, require("../../admin/adminHome").processPostData).then(function() + { + result.redirect("/admin"); + }); }); routes.get('*', (request, result) => diff --git a/routes/admin/login.js b/routes/admin/login.js index 22c208f..f57764b 100644 --- a/routes/admin/login.js +++ b/routes/admin/login.js @@ -10,6 +10,7 @@ routes.get('/', (request, result) => routes.post('/', (request, result) => { + console.log("login request"); utils.constructAdminPage(request, result, require("../../admin/login").main) }); diff --git a/routes/admin/posts.js b/routes/admin/posts.js index 5c68232..28afb3b 100644 --- a/routes/admin/posts.js +++ b/routes/admin/posts.js @@ -1,8 +1,15 @@ const routes = require('express').Router(); +const utils = require('../../utils/utils.js'); + routes.get('/', (request, result) => { + utils.constructAdminPage(request, result, require("../../admin/posts").main) +}); +routes.post('/', (request, result) => +{ + utils.adminPostPageWithOutput(request, result ,require("../../admin/posts").processPostData) }); module.exports = routes; \ No newline at end of file diff --git a/utils/utils.js b/utils/utils.js index b43cbbf..07c3efb 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -135,6 +135,7 @@ module.exports= if(module.exports.loggedIn(request)) { + templateContext.loggedIn = true; promises.push(templateFiller(templateContext)); } else @@ -156,8 +157,56 @@ module.exports= }); }, - adminPostRoute: function(request, result, templateFiller) + adminPostPageWithOutput: function(request, result, templateFiller) { + module.exports.getPostData(request).then(function(postData) + { + var templateContext = Object(); + var promises = []; + + promises.push(includes.fetchTemplate("admin/adminMain.html")); + promises.push(includes.printAdminHeader(templateContext)); + promises.push(includes.printFooter(templateContext)); + + if (module.exports.loggedIn(request)) + { + templateContext.loggedIn = true; + promises.push(templateFiller(templateContext, postData)); + } + else + { + //login + const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0] + || request.connection.remoteAddress; + promises.push(require("../admin/login").main(request, clientAddress, templateContext)); + } + + Promise.all(promises).then(function (content) + { + result.write(whiskers.render(content[0], templateContext)); + result.end(); + }).catch(function (err) { + console.log(err); + throw err; + }); + }); + }, + + adminPostRoute: function(request, templateFiller) + { + return new Promise(function(resolve, reject) + { + module.exports.getPostData(request).then(function(postData) + { + templateFiller(postData).then(function() + { + resolve(); + }) + }).catch(function(e) + { + reject(e); + }) + }) }, From 44c9a284036fa9624ba0b58159a625f3fbefa011 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Mon, 4 Mar 2019 21:38:17 -0500 Subject: [PATCH 06/11] Updated analytics and users page to work with routes. --- admin/analytics.js | 6 +++--- admin/users.js | 13 ++++++------- routes/admin/analytics.js | 4 +++- routes/admin/users.js | 7 +++++++ utils/utils.js | 2 +- 5 files changed, 20 insertions(+), 12 deletions(-) diff --git a/admin/analytics.js b/admin/analytics.js index 3877d51..727f3e7 100644 --- a/admin/analytics.js +++ b/admin/analytics.js @@ -50,17 +50,17 @@ module.exports= * Fetches context information for the admin blog page and handles post * data sent regarding editing blog. * - * @param postData posted by user * @param templateContext json object used as the template context * @returns {Promise} renders the template used for this page */ - main: function(postData, templateContext) + main: function(templateContext) { return new Promise(function(resolve, reject) { Promise.all([includes.fetchTemplate(TEMPLATE_FILE), generateData(templateContext)]).then(function(template) { - resolve(template[0]); + templateContext.adminPage = template[0]; + resolve(); }).catch(function(error) { console.log("error in add admin blog.js"); diff --git a/admin/users.js b/admin/users.js index 74fecde..72b6139 100644 --- a/admin/users.js +++ b/admin/users.js @@ -154,17 +154,15 @@ module.exports= * @param templateContext json object used as the template context * @returns {Promise} renders the template used for this page */ - main: function(postData, templateContext) + main: function(templateContext) { return new Promise(function(resolve, reject) { Promise.all([includes.fetchTemplate(TEMPLATE_FILE), - addUserPostData(postData), - removeUserPost(postData), - editUserPost(postData, templateContext), getUserInformation(templateContext)]).then(function(template) { - resolve(template[0]); + templateContext.adminPage = template[0]; + resolve(); }).catch(function(error) { console.log("error in users.js"); @@ -173,7 +171,7 @@ module.exports= }); }, - processPostData: function(postData, templateContext) + processPostData: function(templateContext, postData) { return new Promise(function(resolve, reject) { @@ -183,7 +181,8 @@ module.exports= editUserPost(postData, templateContext), getUserInformation(templateContext)]).then(function(template) { - resolve(template[0]); + templateContext.adminPage = template[0]; + resolve(); }).catch(function(error) { console.log("error in users.js"); diff --git a/routes/admin/analytics.js b/routes/admin/analytics.js index 5c68232..14b0e06 100644 --- a/routes/admin/analytics.js +++ b/routes/admin/analytics.js @@ -1,8 +1,10 @@ const routes = require('express').Router(); +const utils = require('../../utils/utils.js'); + routes.get('/', (request, result) => { - + utils.constructAdminPage(request, result, require("../../admin/analytics").main) }); module.exports = routes; \ No newline at end of file diff --git a/routes/admin/users.js b/routes/admin/users.js index 5c68232..b9048ec 100644 --- a/routes/admin/users.js +++ b/routes/admin/users.js @@ -1,8 +1,15 @@ const routes = require('express').Router(); +const utils = require('../../utils/utils.js'); + routes.get('/', (request, result) => { + utils.constructAdminPage(request, result, require("../../admin/users").main) +}); +routes.post('/', (request, result) => +{ + utils.adminPostPageWithOutput(request, result ,require("../../admin/users").processPostData) }); module.exports = routes; \ No newline at end of file diff --git a/utils/utils.js b/utils/utils.js index 07c3efb..ed982e6 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -192,6 +192,7 @@ module.exports= }); }, + adminPostRoute: function(request, templateFiller) { return new Promise(function(resolve, reject) @@ -207,7 +208,6 @@ module.exports= reject(e); }) }) - }, From e30ecec170593aa90ccc45d567c52c05e16447c4 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Mon, 4 Mar 2019 21:44:10 -0500 Subject: [PATCH 07/11] Updated downloads page to work with routes. --- admin/adminDownloads.js | 7 ++++--- routes/admin/downloads.js | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/admin/adminDownloads.js b/admin/adminDownloads.js index 717a43d..e96583f 100644 --- a/admin/adminDownloads.js +++ b/admin/adminDownloads.js @@ -116,7 +116,8 @@ module.exports= Promise.all([includes.fetchTemplate(TEMPLATE_FILE), displayDownloads(templateContext)]).then(function(template) { - resolve(template[0]); + templateContext.adminPage = template[0]; + resolve(); }).catch(function(error) { console.log("error in add downloads.js"); @@ -125,7 +126,7 @@ module.exports= }); }, - processPostData: function(postData, templateContext) + processPostData: function(postData) { return new Promise(function(resolve, reject) { @@ -135,7 +136,7 @@ module.exports= resolve(); }).catch(function(error) { - console.log(error); + console.log("Error in admin downloads"); reject(error); }) }) diff --git a/routes/admin/downloads.js b/routes/admin/downloads.js index 5c68232..73ad62b 100644 --- a/routes/admin/downloads.js +++ b/routes/admin/downloads.js @@ -1,8 +1,18 @@ const routes = require('express').Router(); +const utils = require('../../utils/utils.js'); + routes.get('/', (request, result) => { + utils.constructAdminPage(request, result, require("../../admin/adminDownloads").main) +}); +routes.post('/', (request, result) => +{ + utils.adminPostRoute(request ,require("../../admin/adminDownloads").processPostData).then(function() + { + result.redirect('/admin/downloads'); + }) }); module.exports = routes; \ No newline at end of file From 4ed7ad10da34a491d51708a4f025e52aae0e4343 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Tue, 5 Mar 2019 19:11:12 -0500 Subject: [PATCH 08/11] Got the category pages to work under the routes system. --- admin/adminHome.js | 2 +- blog/category.js | 30 +++--- routes/admin/analytics.js | 4 +- routes/admin/downloads.js | 6 +- routes/admin/index.js | 8 +- routes/admin/login.js | 7 +- routes/admin/posts.js | 6 +- routes/admin/users.js | 6 +- routes/category/index.js | 6 +- routes/index.js | 7 ++ sites/admin.js | 44 --------- sites/blog.js | 108 +++++++++----------- utils/pageBuilder.js | 203 ++++++++++++++++++++++++++++++++++++++ utils/utils.js | 114 --------------------- 14 files changed, 293 insertions(+), 258 deletions(-) delete mode 100644 sites/admin.js create mode 100644 utils/pageBuilder.js diff --git a/admin/adminHome.js b/admin/adminHome.js index 42f07ab..50f565a 100644 --- a/admin/adminHome.js +++ b/admin/adminHome.js @@ -96,7 +96,7 @@ const processPost = function(postData) } else if(post.clear_cache) { - require("../sites/blog.js").clearCache(); + require("../utils/pageBuilder").clearCache(); require("../includes/includes.js").clearCache(); resolve(); } diff --git a/blog/category.js b/blog/category.js index a9eb476..68d609e 100644 --- a/blog/category.js +++ b/blog/category.js @@ -16,34 +16,26 @@ module.exports= */ main: function(requestURL, request, templateContext) { + console.log(requestURL); return new Promise(function(resolve, reject) { var page = request.query.page; - const splitURL = requestURL.split("/"); - if(splitURL.length >= 3) - { + const category = requestURL.split("/").join(""); - sql.getPostsFromCategory(splitURL[2]).then(function(posts) - { - Promise.all([blogBodyRenderer.renderBatchOfPosts(requestURL, posts, page, 5, templateContext), - require('./renderNextBar').main(requestURL, page, 5, posts.length, templateContext)]).then(function() - { - resolve(); - }); - }).catch(function() + sql.getPostsFromCategory(category).then(function(posts) + { + Promise.all([blogBodyRenderer.renderBatchOfPosts(requestURL, posts, page, 5, templateContext), + require('./renderNextBar').main(requestURL, page, 5, posts.length, templateContext)]).then(function() { - delete templateContext["posts"]; resolve(); }); - } - - else + }).catch(function() { - //page is not found but, posts list will be empty - // so 404 will display - resolve(); - } + delete templateContext["posts"]; + reject(); + }); + }); } }; \ No newline at end of file diff --git a/routes/admin/analytics.js b/routes/admin/analytics.js index 14b0e06..b2882b7 100644 --- a/routes/admin/analytics.js +++ b/routes/admin/analytics.js @@ -1,10 +1,10 @@ const routes = require('express').Router(); -const utils = require('../../utils/utils.js'); +const builder = require('../../utils/pageBuilder'); routes.get('/', (request, result) => { - utils.constructAdminPage(request, result, require("../../admin/analytics").main) + builder.constructAdminPage(request, result, require("../../admin/analytics").main) }); module.exports = routes; \ No newline at end of file diff --git a/routes/admin/downloads.js b/routes/admin/downloads.js index 73ad62b..5f335b2 100644 --- a/routes/admin/downloads.js +++ b/routes/admin/downloads.js @@ -1,15 +1,15 @@ const routes = require('express').Router(); -const utils = require('../../utils/utils.js'); +const builder = require('../../utils/pageBuilder'); routes.get('/', (request, result) => { - utils.constructAdminPage(request, result, require("../../admin/adminDownloads").main) + builder.constructAdminPage(request, result, require("../../admin/adminDownloads").main) }); routes.post('/', (request, result) => { - utils.adminPostRoute(request ,require("../../admin/adminDownloads").processPostData).then(function() + builder.adminPostRoute(request ,require("../../admin/adminDownloads").processPostData).then(function() { result.redirect('/admin/downloads'); }) diff --git a/routes/admin/index.js b/routes/admin/index.js index c0e2768..04bdc89 100644 --- a/routes/admin/index.js +++ b/routes/admin/index.js @@ -15,16 +15,16 @@ routes.use('/downloads', downloads); const users = require('./users'); routes.use('/users', users); -const utils = require('../../utils/utils.js'); +const builder = require('../../utils/pageBuilder.js'); routes.get('/', (request, result) => { - utils.constructAdminPage(request, result, require("../../admin/adminHome").main) + builder.constructAdminPage(request, result, require("../../admin/adminHome").main) }); routes.post('/', (request, result) => { - utils.adminPostRoute(request, require("../../admin/adminHome").processPostData).then(function() + builder.adminPostRoute(request, require("../../admin/adminHome").processPostData).then(function() { result.redirect("/admin"); }); @@ -32,7 +32,7 @@ routes.post('/', (request, result) => routes.get('*', (request, result) => { - utils.print404(result); + builder.print404(result); }); module.exports = routes; \ No newline at end of file diff --git a/routes/admin/login.js b/routes/admin/login.js index f57764b..65324c2 100644 --- a/routes/admin/login.js +++ b/routes/admin/login.js @@ -1,17 +1,16 @@ const routes = require('express').Router(); -const utils = require('../../utils/utils.js'); +const builder = require('../../utils/pageBuilder.js'); routes.get('/', (request, result) => { - utils.constructAdminPage(request, result, require("../../admin/adminHome").main) + builder.constructAdminPage(request, result, require("../../admin/adminHome").main) }); routes.post('/', (request, result) => { - console.log("login request"); - utils.constructAdminPage(request, result, require("../../admin/login").main) + builder.constructAdminPage(request, result, require("../../admin/login").main) }); module.exports = routes; \ No newline at end of file diff --git a/routes/admin/posts.js b/routes/admin/posts.js index 28afb3b..7699d5b 100644 --- a/routes/admin/posts.js +++ b/routes/admin/posts.js @@ -1,15 +1,15 @@ const routes = require('express').Router(); -const utils = require('../../utils/utils.js'); +const builder = require('../../utils/pageBuilder'); routes.get('/', (request, result) => { - utils.constructAdminPage(request, result, require("../../admin/posts").main) + builder.constructAdminPage(request, result, require("../../admin/posts").main) }); routes.post('/', (request, result) => { - utils.adminPostPageWithOutput(request, result ,require("../../admin/posts").processPostData) + builder.adminPostPageWithOutput(request, result ,require("../../admin/posts").processPostData) }); module.exports = routes; \ No newline at end of file diff --git a/routes/admin/users.js b/routes/admin/users.js index b9048ec..c252497 100644 --- a/routes/admin/users.js +++ b/routes/admin/users.js @@ -1,15 +1,15 @@ const routes = require('express').Router(); -const utils = require('../../utils/utils.js'); +const builder = require('../../utils/pageBuilder.js'); routes.get('/', (request, result) => { - utils.constructAdminPage(request, result, require("../../admin/users").main) + builder.constructAdminPage(request, result, require("../../admin/users").main) }); routes.post('/', (request, result) => { - utils.adminPostPageWithOutput(request, result ,require("../../admin/users").processPostData) + builder.adminPostPageWithOutput(request, result ,require("../../admin/users").processPostData) }); module.exports = routes; \ No newline at end of file diff --git a/routes/category/index.js b/routes/category/index.js index 5c68232..7339d18 100644 --- a/routes/category/index.js +++ b/routes/category/index.js @@ -1,8 +1,10 @@ const routes = require('express').Router(); -routes.get('/', (request, result) => -{ +const pageBuilder = require('../../utils/pageBuilder'); +routes.get('*', (request, result) => +{ + pageBuilder.buildBlogPage(request, result, require("../../blog/category").main) }); module.exports = routes; \ No newline at end of file diff --git a/routes/index.js b/routes/index.js index 74e0fae..a5f8451 100644 --- a/routes/index.js +++ b/routes/index.js @@ -20,6 +20,13 @@ routes.use('/downloads', downloads); const includes = require('./includes'); routes.use('/includes', includes); + +/** category pages */ +const category = require('./category'); +routes.use('/category', category); + + + const projects = ["/steam/"]; diff --git a/sites/admin.js b/sites/admin.js deleted file mode 100644 index 9b9ea2a..0000000 --- a/sites/admin.js +++ /dev/null @@ -1,44 +0,0 @@ -//sending static content -const includes = require('../includes/includes.js'); - -const whiskers = require('whiskers'); - - -/** - * @author Jeffery Russell 11-3-18 - * - * @type {{main: (function(*=, *): Promise)}} - */ -module.exports= - { - /** - * Calls blog and sidebar modules to render blog contents in order - * - * @param requestURL - * @returns {Promise|*} - */ - main: function(request, result, filename) - { - const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0] - || request.connection.remoteAddress; - - result.writeHead(200, {'Content-Type': 'text/html'}); - - const file = "../admin/admin.js"; - - var templateContext = Object(); - Promise.all([includes.fetchTemplate("admin/adminMain.html"), - includes.printAdminHeader(templateContext), - require(file).main(request, clientAddress, templateContext, filename), - includes.printFooter(templateContext), - ]).then(function(content) - { - result.write(whiskers.render(content[0], templateContext)); - result.end(); - }).catch(function(err) - { - console.log(err); - throw err; - }); - } - }; \ No newline at end of file diff --git a/sites/blog.js b/sites/blog.js index 3d798ae..6b59187 100644 --- a/sites/blog.js +++ b/sites/blog.js @@ -34,77 +34,67 @@ module.exports= */ main: function(request, result, filename) { - if(contentLoader.serveStaticContent(request, result, filename, "")) - { - //do nothing - } - //downloads - else if (filename.includes("/downloads/")) - { - require("../includes/downloads.js").main(result, filename); - } - else - { - var page = request.query.page; - if(typeof page == "undefined") - page = 1; - page = Number(page); - const html = cache.get(filename + "?page=" + page); + var page = request.query.page; + if(typeof page == "undefined") + page = 1; + page = Number(page); + + const html = cache.get(filename + "?page=" + page); - result.writeHead(200, {'Content-Type': 'text/html'}); - if (html == null) + result.writeHead(200, {'Content-Type': 'text/html'}); + if (html == null) + { + var file = ""; + + if (filename === '' || filename === '/') { - var file = ""; + file = "../blog/homePage.js"; + } + else + { + var urlSplit = filename.split("/"); - if (filename === '' || filename === '/') - { - file = "../blog/homePage.js"; - } + if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page + file = "../blog/category.js"; else { - var urlSplit = filename.split("/"); - - if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page - file = "../blog/category.js"; - else - { - file = "../blog/posts.js"; - page = 1; // all blog are single page, everyone must be one to ensure - // cache is not tricked into storing same blog post a ton of times - } + file = "../blog/posts.js"; + page = 1; // all blog are single page, everyone must be one to ensure + // cache is not tricked into storing same blog post a ton of times } - - var templateContext = Object(); - Promise.all([includes.fetchTemplate(TEMPLATE_FILE), - utils.includeInObject(PAGINATION_TEMPLATE_KEY, templateContext, "templates/" + PAGINATION_TEMPLATE_FILE), - includes.printHeader(templateContext), - includes.printFooter(templateContext), - require(file).main(filename, request, templateContext), - require("../blog/sidebar.js").main(templateContext)]) - .then(function (content) - { - const html = whiskers.render(content[0], templateContext); - result.write(html); - result.end(); - cache.put(filename + "?page=" + page, html); - }).catch(function (err) - { - console.log(err); - cache.del(filename + "?page=" + page); - utils.print404().then(function(content) - { - result.write(content); - result.end(); - }) - }); } - else + + var templateContext = Object(); + Promise.all([includes.fetchTemplate(TEMPLATE_FILE), + utils.includeInObject(PAGINATION_TEMPLATE_KEY, templateContext, "templates/" + PAGINATION_TEMPLATE_FILE), + includes.printHeader(templateContext), + includes.printFooter(templateContext), + require(file).main(filename, request, templateContext), + require("../blog/sidebar.js").main(templateContext)]) + .then(function (content) { + const html = whiskers.render(content[0], templateContext); result.write(html); result.end(); - } + cache.put(filename + "?page=" + page, html); + }).catch(function (err) + { + console.log(err); + cache.del(filename + "?page=" + page); + utils.print404().then(function(content) + { + result.write(content); + result.end(); + }) + }); } + else + { + result.write(html); + result.end(); + } + }, /** diff --git a/utils/pageBuilder.js b/utils/pageBuilder.js new file mode 100644 index 0000000..f6cce9d --- /dev/null +++ b/utils/pageBuilder.js @@ -0,0 +1,203 @@ +const utils = require('./utils'); + +const whiskers = require('whiskers'); + +const includes = require("../includes/includes"); + +const cache = require('memory-cache'); + +/** used to parse the request URL */ +const url = require('url'); + + +const TEMPLATE_FILE="blog/blogMain.html"; + + +const PAGINATION_TEMPLATE_KEY = "paginationTemplate"; +const PAGINATION_TEMPLATE_FILE = "blog/paginationBar.html"; + + +module.exports = + { + /** + * Displays 404 error to user + * + * @param result + * @returns {*} + */ + print404: function(result) + { + includes.include("includes/html/404.html").then(function(html) + { + result.write(html); + result.end(); + }) + }, + + + constructAdminPage: function(request, result, templateFiller) + { + var templateContext = Object(); + var promises = []; + + promises.push(includes.fetchTemplate("admin/adminMain.html")); + promises.push(includes.printAdminHeader(templateContext)); + promises.push(includes.printFooter(templateContext)); + + if(module.exports.loggedIn(request)) + { + templateContext.loggedIn = true; + promises.push(templateFiller(templateContext)); + } + else + { + //login + const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0] + || request.connection.remoteAddress; + promises.push(require("../admin/login").main(request, clientAddress,templateContext)); + } + + Promise.all(promises).then(function(content) + { + result.write(whiskers.render(content[0], templateContext)); + result.end(); + }).catch(function(err) + { + console.log(err); + throw err; + }); + }, + + adminPostPageWithOutput: function(request, result, templateFiller) + { + utils.getPostData(request).then(function(postData) + { + var templateContext = Object(); + var promises = []; + + promises.push(includes.fetchTemplate("admin/adminMain.html")); + promises.push(includes.printAdminHeader(templateContext)); + promises.push(includes.printFooter(templateContext)); + + if (module.exports.loggedIn(request)) + { + templateContext.loggedIn = true; + promises.push(templateFiller(templateContext, postData)); + } + else + { + //login + const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0] + || request.connection.remoteAddress; + promises.push(require("../admin/login").main(request, clientAddress, templateContext)); + } + + Promise.all(promises).then(function (content) + { + result.write(whiskers.render(content[0], templateContext)); + result.end(); + }).catch(function (err) { + console.log(err); + throw err; + }); + }); + }, + + + adminPostRoute: function(request, templateFiller) + { + return new Promise(function(resolve, reject) + { + utils.getPostData(request).then(function(postData) + { + templateFiller(postData).then(function() + { + resolve(); + }) + }).catch(function(e) + { + reject(e); + }) + }) + }, + + + loggedIn(request) + { + return(request.session && request.session.user); + }, + + + buildBlogPage: function(request, result, templateFiller) + { + var page = request.query.page; + if(typeof page == "undefined") + page = 1; + page = Number(page); + + var filename = url.parse(request.url, true).pathname; + + const html = cache.get(filename + "?page=" + page); + + result.writeHead(200, {'Content-Type': 'text/html'}); + if (html == null) + { + // var file = ""; + // + // if (filename === '' || filename === '/') + // { + // file = "../blog/homePage.js"; + // } + // else + // { + // var urlSplit = filename.split("/"); + // + // if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page + // file = "../blog/category.js"; + // else + // { + // file = "../blog/posts.js"; + // page = 1; // all blog are single page, everyone must be one to ensure + // // cache is not tricked into storing same blog post a ton of times + // } + // } + + var templateContext = Object(); + Promise.all([includes.fetchTemplate(TEMPLATE_FILE), + includes.includeInObject(PAGINATION_TEMPLATE_KEY, templateContext, "templates/" + PAGINATION_TEMPLATE_FILE), + includes.printHeader(templateContext), + includes.printFooter(templateContext), + templateFiller(filename, request, templateContext), + require("../blog/sidebar.js").main(templateContext)]) + .then(function (content) + { + console.log(JSON.stringify(content)); + const html = whiskers.render(content[0], templateContext); + result.write(html); + result.end(); + cache.put(filename + "?page=" + page, html); + }).catch(function (err) + { + console.log("dewie"); + console.log(err); + cache.del(filename + "?page=" + page); + + module.exports.print404(result) + }); + } + else + { + result.write(html); + result.end(); + } + }, + + /** + * Clears the memory cache. + */ + clearCache: function() + { + console.log("Blog cache cleared"); + cache.clear(); + } + }; \ No newline at end of file diff --git a/utils/utils.js b/utils/utils.js index ed982e6..fca1aec 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -6,11 +6,6 @@ //used for file io const fs = require('fs'); -const whiskers = require('whiskers'); - - -const includes = require("../includes/includes"); - module.exports= { @@ -106,113 +101,4 @@ module.exports= } }); }, - - - /** - * Displays 404 error to user - * - * @param result - * @returns {*} - */ - print404: function(result) - { - includes.include("includes/html/404.html").then(function(html) - { - result.write(html); - result.end(); - }) - }, - - - constructAdminPage: function(request, result, templateFiller) - { - var templateContext = Object(); - var promises = []; - - promises.push(includes.fetchTemplate("admin/adminMain.html")); - promises.push(includes.printAdminHeader(templateContext)); - promises.push(includes.printFooter(templateContext)); - - if(module.exports.loggedIn(request)) - { - templateContext.loggedIn = true; - promises.push(templateFiller(templateContext)); - } - else - { - //login - const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0] - || request.connection.remoteAddress; - promises.push(require("../admin/login").main(request, clientAddress,templateContext)); - } - - Promise.all(promises).then(function(content) - { - result.write(whiskers.render(content[0], templateContext)); - result.end(); - }).catch(function(err) - { - console.log(err); - throw err; - }); - }, - - adminPostPageWithOutput: function(request, result, templateFiller) - { - module.exports.getPostData(request).then(function(postData) - { - var templateContext = Object(); - var promises = []; - - promises.push(includes.fetchTemplate("admin/adminMain.html")); - promises.push(includes.printAdminHeader(templateContext)); - promises.push(includes.printFooter(templateContext)); - - if (module.exports.loggedIn(request)) - { - templateContext.loggedIn = true; - promises.push(templateFiller(templateContext, postData)); - } - else - { - //login - const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0] - || request.connection.remoteAddress; - promises.push(require("../admin/login").main(request, clientAddress, templateContext)); - } - - Promise.all(promises).then(function (content) - { - result.write(whiskers.render(content[0], templateContext)); - result.end(); - }).catch(function (err) { - console.log(err); - throw err; - }); - }); - }, - - - adminPostRoute: function(request, templateFiller) - { - return new Promise(function(resolve, reject) - { - module.exports.getPostData(request).then(function(postData) - { - templateFiller(postData).then(function() - { - resolve(); - }) - }).catch(function(e) - { - reject(e); - }) - }) - }, - - - loggedIn(request) - { - return(request.session && request.session.user); - } }; \ No newline at end of file From a5a51bf1e190537660a87f6b4713218efdb40aac Mon Sep 17 00:00:00 2001 From: jrtechs Date: Tue, 5 Mar 2019 20:37:22 -0500 Subject: [PATCH 09/11] Got blog posts working with the routes system. --- blog/category.js | 2 +- blog/renderBlogPost.js | 2 +- routes/index.js | 13 +++-- sites/blog.js | 108 ----------------------------------------- utils/pageBuilder.js | 42 +++++----------- 5 files changed, 21 insertions(+), 146 deletions(-) delete mode 100644 sites/blog.js diff --git a/blog/category.js b/blog/category.js index 68d609e..80fe24d 100644 --- a/blog/category.js +++ b/blog/category.js @@ -26,7 +26,7 @@ module.exports= sql.getPostsFromCategory(category).then(function(posts) { Promise.all([blogBodyRenderer.renderBatchOfPosts(requestURL, posts, page, 5, templateContext), - require('./renderNextBar').main(requestURL, page, 5, posts.length, templateContext)]).then(function() + require('./renderNextBar').main("/category" + request.url, page, 5, posts.length, templateContext)]).then(function() { resolve(); }); diff --git a/blog/renderBlogPost.js b/blog/renderBlogPost.js index 223c8a7..1798c66 100644 --- a/blog/renderBlogPost.js +++ b/blog/renderBlogPost.js @@ -255,4 +255,4 @@ module.exports= }); }); } - } + }; diff --git a/routes/index.js b/routes/index.js index a5f8451..8b63124 100644 --- a/routes/index.js +++ b/routes/index.js @@ -25,23 +25,22 @@ routes.use('/includes', includes); const category = require('./category'); routes.use('/category', category); - +const pageBuilder = require('../utils/pageBuilder'); const projects = ["/steam/"]; - - +//blog home page routes.get('/', (request, result) => { - //blog home page + pageBuilder.buildBlogPage(request, result, require("../blog/homePage").main) }); - +//regular blog posts routes.get('*', (request, result) => { - console.log("Un registered event.") + pageBuilder.buildBlogPage(request, result, + require("../blog/posts").main, request.url); }); - module.exports = routes; \ No newline at end of file diff --git a/sites/blog.js b/sites/blog.js deleted file mode 100644 index 6b59187..0000000 --- a/sites/blog.js +++ /dev/null @@ -1,108 +0,0 @@ -//used to append static content to result -const includes = require('../includes/includes.js'); - -//used to append static content to result -const contentLoader = require('../includes/staticContentServer.js'); - -const whiskers = require('whiskers'); - -const TEMPLATE_FILE="blog/blogMain.html"; - - -const PAGINATION_TEMPLATE_KEY = "paginationTemplate"; -const PAGINATION_TEMPLATE_FILE = "blog/paginationBar.html"; - - -//caching program to make the application run faster -const cache = require('memory-cache'); - -//file io -const utils = require('../utils/utils.js'); - -/** - * @author Jeffery Russell 11-3-18 - * - * @type {{main: (function(*=, *): Promise)}} - */ -module.exports= - { - /** - * Calls posts and sidebar modules to render blog contents in order - * - * @param requestURL - * @returns {Promise|*} - */ - main: function(request, result, filename) - { - - var page = request.query.page; - if(typeof page == "undefined") - page = 1; - page = Number(page); - - const html = cache.get(filename + "?page=" + page); - - result.writeHead(200, {'Content-Type': 'text/html'}); - if (html == null) - { - var file = ""; - - if (filename === '' || filename === '/') - { - file = "../blog/homePage.js"; - } - else - { - var urlSplit = filename.split("/"); - - if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page - file = "../blog/category.js"; - else - { - file = "../blog/posts.js"; - page = 1; // all blog are single page, everyone must be one to ensure - // cache is not tricked into storing same blog post a ton of times - } - } - - var templateContext = Object(); - Promise.all([includes.fetchTemplate(TEMPLATE_FILE), - utils.includeInObject(PAGINATION_TEMPLATE_KEY, templateContext, "templates/" + PAGINATION_TEMPLATE_FILE), - includes.printHeader(templateContext), - includes.printFooter(templateContext), - require(file).main(filename, request, templateContext), - require("../blog/sidebar.js").main(templateContext)]) - .then(function (content) - { - const html = whiskers.render(content[0], templateContext); - result.write(html); - result.end(); - cache.put(filename + "?page=" + page, html); - }).catch(function (err) - { - console.log(err); - cache.del(filename + "?page=" + page); - utils.print404().then(function(content) - { - result.write(content); - result.end(); - }) - }); - } - else - { - result.write(html); - result.end(); - } - - }, - - /** - * Clears the memory cache. - */ - clearCache: function() - { - console.log("Blog cache cleared"); - cache.clear(); - } - }; \ No newline at end of file diff --git a/utils/pageBuilder.js b/utils/pageBuilder.js index f6cce9d..198d227 100644 --- a/utils/pageBuilder.js +++ b/utils/pageBuilder.js @@ -128,40 +128,18 @@ module.exports = }, - buildBlogPage: function(request, result, templateFiller) + buildBlogPageWithURL: function(request, result, templateFiller, filename) { var page = request.query.page; if(typeof page == "undefined") page = 1; page = Number(page); - var filename = url.parse(request.url, true).pathname; - const html = cache.get(filename + "?page=" + page); result.writeHead(200, {'Content-Type': 'text/html'}); if (html == null) { - // var file = ""; - // - // if (filename === '' || filename === '/') - // { - // file = "../blog/homePage.js"; - // } - // else - // { - // var urlSplit = filename.split("/"); - // - // if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page - // file = "../blog/category.js"; - // else - // { - // file = "../blog/posts.js"; - // page = 1; // all blog are single page, everyone must be one to ensure - // // cache is not tricked into storing same blog post a ton of times - // } - // } - var templateContext = Object(); Promise.all([includes.fetchTemplate(TEMPLATE_FILE), includes.includeInObject(PAGINATION_TEMPLATE_KEY, templateContext, "templates/" + PAGINATION_TEMPLATE_FILE), @@ -177,13 +155,13 @@ module.exports = result.end(); cache.put(filename + "?page=" + page, html); }).catch(function (err) - { - console.log("dewie"); - console.log(err); - cache.del(filename + "?page=" + page); + { + console.log("dewie"); + console.log(err); + cache.del(filename + "?page=" + page); - module.exports.print404(result) - }); + module.exports.print404(result) + }); } else { @@ -192,6 +170,12 @@ module.exports = } }, + buildBlogPage: function(request, result, templateFiller) + { + var filename = url.parse(request.url, true).pathname; + module.exports.buildBlogPageWithURL(request, result,templateFiller, filename); + }, + /** * Clears the memory cache. */ From b0a911d7dca70505f244261ffb9353d665d97007 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 9 Mar 2019 12:43:17 -0500 Subject: [PATCH 10/11] Fixed the static content server to dish up static images from the blog posts. --- includes/includes.js | 4 ---- includes/staticContentServer.js | 4 +++- routes/includes/index.js | 8 ++++++-- routes/index.js | 1 + utils/pageBuilder.js | 3 --- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/includes/includes.js b/includes/includes.js index d7365de..4e7a32c 100644 --- a/includes/includes.js +++ b/includes/includes.js @@ -7,9 +7,6 @@ * @author Jeffery Russell */ -//used for file IO -const utils = require('../utils/utils.js'); - const HEADER_KEY = "header"; const FOOTER_KEY = "footer"; @@ -112,7 +109,6 @@ module.exports = { readFile(fileName).then(function(result) { - console.log(result); context[key] = result; resolve(); }).catch(function(error) diff --git a/includes/staticContentServer.js b/includes/staticContentServer.js index 905ddc4..03952ae 100644 --- a/includes/staticContentServer.js +++ b/includes/staticContentServer.js @@ -12,7 +12,9 @@ module.exports= serveStaticContent: function(request, result, filename, baseURL) { - fullPath = baseURL + filename; + var fullPath = baseURL + filename; + if(fullPath.charAt(0) === '/') + fullPath = fullPath.substring(1,); if (filename.includes(".svg") || filename.includes(".svg")) { includes.sendSVG(result, fullPath); diff --git a/routes/includes/index.js b/routes/includes/index.js index 70a5ddf..31b705d 100644 --- a/routes/includes/index.js +++ b/routes/includes/index.js @@ -10,9 +10,13 @@ const contentLoader = require('../../includes/staticContentServer.js'); routes.get('*', (request, result) => { var filename = url.parse(request.url, true).pathname; - - if(!contentLoader.serveStaticContent(request, result, filename, "includes")) + console.log(request.originalURL); + console.log(request.url.path); + if(!contentLoader.serveStaticContent(request, result, request.originalUrl, "")) { + + + //missing content } }); diff --git a/routes/index.js b/routes/index.js index 8b63124..b453916 100644 --- a/routes/index.js +++ b/routes/index.js @@ -19,6 +19,7 @@ routes.use('/downloads', downloads); /** static content like css, js, and images */ const includes = require('./includes'); routes.use('/includes', includes); +routes.use('/blogContent', includes); /** category pages */ diff --git a/utils/pageBuilder.js b/utils/pageBuilder.js index 198d227..b9c55db 100644 --- a/utils/pageBuilder.js +++ b/utils/pageBuilder.js @@ -149,15 +149,12 @@ module.exports = require("../blog/sidebar.js").main(templateContext)]) .then(function (content) { - console.log(JSON.stringify(content)); const html = whiskers.render(content[0], templateContext); result.write(html); result.end(); cache.put(filename + "?page=" + page, html); }).catch(function (err) { - console.log("dewie"); - console.log(err); cache.del(filename + "?page=" + page); module.exports.print404(result) From 4963cbe0aaac64ac5885c0f7c0703f38a898d063 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 9 Mar 2019 13:33:35 -0500 Subject: [PATCH 11/11] Got the project sites working and fixed bug with pagination bar. --- blog/renderNextBar.js | 8 ++++-- includes/includes.js | 2 +- routes/includes/index.js | 10 ------- routes/index.js | 4 ++- routes/projects/index.js | 24 +++++++++++++++- server.js | 59 ---------------------------------------- sites/projects.js | 1 - 7 files changed, 32 insertions(+), 76 deletions(-) diff --git a/blog/renderNextBar.js b/blog/renderNextBar.js index 4453ae4..6933f7e 100644 --- a/blog/renderNextBar.js +++ b/blog/renderNextBar.js @@ -40,14 +40,16 @@ module.exports= var nextPage = currentPage + 1; var previousPage = currentPage - 1; + + var strippedURL = baseURL.split("?page=")[0]; if (isValidPage(previousPage, postsPerPage, totalPosts)) { - paginationObject.previous = {url: baseURL + "?page=" + previousPage}; + paginationObject.previous = {url: strippedURL + "?page=" + previousPage}; } if (isValidPage(nextPage, postsPerPage, totalPosts)) { - paginationObject.next = {url: baseURL + "?page=" + nextPage}; + paginationObject.next = {url: strippedURL + "?page=" + nextPage}; } var page = 1; @@ -60,7 +62,7 @@ module.exports= } else { - pages.push({number: page, url: baseURL + "?page=" + page}) + pages.push({number: page, url: strippedURL + "?page=" + page}) } page = page + 1; } diff --git a/includes/includes.js b/includes/includes.js index 4e7a32c..910b2d2 100644 --- a/includes/includes.js +++ b/includes/includes.js @@ -201,7 +201,7 @@ module.exports = */ sendHTML: function(result, fileName) { - readFile("." + fileName).then(function(content) + readFile(fileName).then(function(content) { result.writeHead(200, {'Content-Type': 'text/html'}); result.write(content); diff --git a/routes/includes/index.js b/routes/includes/index.js index 31b705d..8503dc6 100644 --- a/routes/includes/index.js +++ b/routes/includes/index.js @@ -1,22 +1,12 @@ const routes = require('express').Router(); -/** used to parse the request URL */ -const url = require('url'); - const contentLoader = require('../../includes/staticContentServer.js'); - - routes.get('*', (request, result) => { - var filename = url.parse(request.url, true).pathname; - console.log(request.originalURL); - console.log(request.url.path); if(!contentLoader.serveStaticContent(request, result, request.originalUrl, "")) { - - //missing content } }); diff --git a/routes/index.js b/routes/index.js index b453916..9256e64 100644 --- a/routes/index.js +++ b/routes/index.js @@ -28,7 +28,9 @@ routes.use('/category', category); const pageBuilder = require('../utils/pageBuilder'); -const projects = ["/steam/"]; +const project = require('./projects'); +routes.use('/steam', project); + //blog home page diff --git a/routes/projects/index.js b/routes/projects/index.js index 5c68232..456e378 100644 --- a/routes/projects/index.js +++ b/routes/projects/index.js @@ -1,8 +1,30 @@ const routes = require('express').Router(); -routes.get('/', (request, result) => +const url = require('url'); + +const contentLoader = require('../../includes/staticContentServer.js'); + +routes.get('*', (request, result) => { + var filename = url.parse(request.url, true).pathname; + var extra = ''; + if(filename === '/') + { + if(request.originalUrl.charAt(request.originalUrl.length -1) !== '/') + { + result.redirect(request.originalUrl + "/"); + } + else + { + extra = "/index.html"; + } + } + + if(!contentLoader.serveStaticContent(request, result, + request.originalUrl + extra, "blogContent/projects")) + { + } }); module.exports = routes; \ No newline at end of file diff --git a/server.js b/server.js index 7e8ea56..c28ba88 100644 --- a/server.js +++ b/server.js @@ -17,13 +17,9 @@ const express = require("express"); /** express app */ const app = express(); -/** server side logging */ -const sql = require('./utils/sql'); - /** Used for gzip compression */ const compression = require('compression'); - /**Updates the site map whenever the server is started */ const map = require('./utils/generateSiteMap.js'); map.main(); @@ -35,64 +31,9 @@ const session = require('express-session'); /**Initializes sessions for login */ app.use(session({ secret: config.SESSION_SECRET, cookie: { maxAge: 6000000 }})); - - - const routes = require('./routes'); app.use('/', routes); -// /** -// * Parses the request url and calls correct JS files -// */ -// app.use(function(request, result) -// { -// //prevents people from pointing their dns at my IP:port for my site -// if(request.headers.host.includes("localhost:" + port) || -// request.headers.host.includes("jrtechs.net")) -// { -// const filename = url.parse(request.url, true).pathname; -// -// var project = false; -// projects.forEach(function(projectName) -// { -// if(filename.startsWith(projectName)) -// { -// require("./sites/projects.js").main(request, result, projectName); -// project = true; -// } -// }); -// -// if(filename.startsWith("/admin")) -// { -// require("./sites/admin.js").main(request, result, filename); -// project = true; -// } -// -// if(!project) -// { -// require("./sites/blog.js").main(request, result, filename); -// } -// -// try -// { -// const getClientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0] -// || request.connection.remoteAddress; -// console.log(getClientAddress); -// -// sql.logTraffic(getClientAddress, filename); -// } -// catch (e) -// { } -// } -// else -// { -// // utils.printWrongHost(result); -// result.writeHead(418, {}); -// result.end(); -// } -// }); - - //enables gzip compression for the site app.use(compression()); diff --git a/sites/projects.js b/sites/projects.js index 366ba47..976511a 100644 --- a/sites/projects.js +++ b/sites/projects.js @@ -29,7 +29,6 @@ module.exports= filename = baseURL + "index.html"; } - if (!contentLoader.serveStaticContent(request, result, filename, "/blogContent/projects")) { console.log(filename);