From aa88b4429ffe790758e65ab9e8ca29a764740004 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 3 Nov 2018 10:44:40 -0400 Subject: [PATCH] Refactored code to use a single place for loading static content such as html, css, js and image files. I also moved the render blog content outside of the server.js to give more readability. --- README.md | 2 +- blogSql.svg => docs/blogSql.svg | 0 includes/includes.js | 51 +++++++++++++-- includes/projects.js | 64 ------------------- includes/staticContentServer.js | 51 +++++++++++++++ server.js | 106 ++++---------------------------- admin.js => sites/admin.js | 4 +- sites/blog.js | 83 +++++++++++++++++++++++++ sites/projects.js | 42 +++++++++++++ 9 files changed, 237 insertions(+), 166 deletions(-) rename blogSql.svg => docs/blogSql.svg (100%) delete mode 100644 includes/projects.js create mode 100644 includes/staticContentServer.js rename admin.js => sites/admin.js (94%) create mode 100644 sites/blog.js create mode 100644 sites/projects.js diff --git a/README.md b/README.md index e83313a..ff502bf 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ unless otherwise stated. ## MYSQL Schema -![](blogSql.svg) +![](docs/blogSql.svg) ```mysql create database jrtechs_blog; diff --git a/blogSql.svg b/docs/blogSql.svg similarity index 100% rename from blogSql.svg rename to docs/blogSql.svg diff --git a/includes/includes.js b/includes/includes.js index 5c635a4..c660bd3 100644 --- a/includes/includes.js +++ b/includes/includes.js @@ -23,6 +23,10 @@ const ADMIN_HEADER = "includes/html/adminHeader.html"; const crypto = require('crypto'); +//caching program to make the application run faster +const cache = require('memory-cache'); + + /** * Sends a static file to the client in a way which the web browser * caches the contents sent. @@ -32,7 +36,7 @@ const crypto = require('crypto'); * @param type -- type of file for the header * @param result -- sent to client */ -const sendCachedContent = function(cache, path, type, result) +const sendCachedContent = function(path, type, result) { const goods = cache.get(path); @@ -45,6 +49,7 @@ const sendCachedContent = function(cache, path, type, result) 'public, max-age=2678400', 'ETag': '"' + eTag + '"', 'Vary': 'Accept-Encoding'}); result.write(content); + console.log(content); result.end(); cache.put(path, content); }).catch(function(error) @@ -108,7 +113,19 @@ module.exports = */ sendCSS: function(result, path, cache) { - sendCachedContent(cache, "/" + path, 'text/css', result); + sendCachedContent(path, 'text/css', result); + }, + + + /**Sends the user an image from the specified fileName. + * + * @param result + * @param fileName + */ + sendImage: function(result, fileName) + { + console.log(fileName); + sendCachedContent(fileName, 'image/png', result); }, @@ -117,9 +134,9 @@ module.exports = * @param result * @param fileName */ - sendImage: function(result, fileName, cache) + sendJS: function(result, fileName) { - sendCachedContent(cache, fileName, 'image/png', result); + sendCachedContent(fileName, 'application/javascript', result); }, @@ -128,8 +145,30 @@ module.exports = * @param result * @param fileName */ - sendJS: function(result, fileName, cache) + sendHTML: function(result, fileName) + { + utils.include("." + fileName).then(function(content) + { + console.log(fileName); + console.log(content); + result.writeHead(200, {'Content-Type': 'text/html'}); + result.write(content); + result.end(); + }).catch(function(error) + { + console.log(error); + }); + }, + + + /** + * Sends a svg file to the client. + * + * @param result + * @param fileName + */ + sendSVG: function(result, fileName) { - sendCachedContent(cache, fileName, 'application/javascript', result); + sendCachedContent(fileName, 'image/svg+xml', result); } }; \ No newline at end of file diff --git a/includes/projects.js b/includes/projects.js deleted file mode 100644 index 22c545e..0000000 --- a/includes/projects.js +++ /dev/null @@ -1,64 +0,0 @@ -//file io -const utils = require('../utils/utils.js'); - -//used to parse the request URL -const url = require('url'); - - -/** - * @author Jeffery Russell 10-30-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, baseURL) - { - //const filename = url.parse(request.url, true).pathname - - var filename = url.parse(request.url, true).pathname; - - if(filename.includes(".svg") || filename.includes(".svg")) - { - result.writeHead(200, {'Content-Type': 'image/svg+xml'}); - } - else if(filename.includes("/img/") || filename.includes(".jpg") || - filename.includes(".png") || filename.includes(".ico")) - { - result.writeHead(200, {'Content-Type': 'image/png'}); - } - else if(filename.includes("/css/") || filename.includes(".woff2") || - filename.includes(".txt")) - { - result.writeHead(200, {'Content-Type': 'text/css'}); - } - else if(filename.includes("/js/") || filename.includes(".js")) - { - result.writeHead(200, {'Content-Type': 'application/javascript'}); - } - else - { - result.writeHead(200, {'Content-Type': 'text/html'}); - } - - if(filename == baseURL || filename == baseURL.substring(0, baseURL.length - 1)) - { - filename = baseURL + "index.html"; - } - - utils.include("./blogContent/projects" + filename).then(function(content) - { - result.write(content); - result.end(); - }).catch(function(error) - { - console.log(error); - }); - } - }; \ No newline at end of file diff --git a/includes/staticContentServer.js b/includes/staticContentServer.js new file mode 100644 index 0000000..407fe7c --- /dev/null +++ b/includes/staticContentServer.js @@ -0,0 +1,51 @@ +//used to append static content to result +const includes = require('../includes/includes.js'); + + + +/** + * @author Jeffery Russell 10-30-18 + * + * @type {{main: (function(*=, *): Promise)}} + */ +module.exports= + { + + serveStaticContent: function(request, result, filename, baseURL) + { + console.log(filename); + if (filename.includes(".svg") || filename.includes(".svg")) + { + includes.sendSVG(result, baseURL + filename); + return true; + } + //handles image requests + else if (filename.includes(".jpg") || + filename.includes(".png") || filename.includes(".ico")) + { + console.log("Da fuck"); + includes.sendImage(result, baseURL + filename); + return true; + } + //css and font files + else if (filename.includes(".woff2") || filename.includes(".css") || + filename.includes(".txt")) + { + includes.sendCSS(result, baseURL + filename); + return true; + } + //scripts + else if (filename.includes("/js/") || filename.includes(".js")) + { + includes.sendJS(result, baseURL + filename); + return true; + } + //html + else if (filename.includes(".html")) + { + includes.sendHTML(result, baseURL + filename); + return true; + } + return false; + } + }; \ No newline at end of file diff --git a/server.js b/server.js index 5c70921..7d0ceb5 100644 --- a/server.js +++ b/server.js @@ -17,17 +17,13 @@ const express = require("express"); //express app const app = express(); -//used to append static content to result -const includes = require('./includes/includes.js'); - //server side logging const sql = require('./utils/sql'); //Used for gzip compression -const compression = require('compression') +const compression = require('compression'); + -//caching program to make the application run faster -const cache = require('memory-cache'); //Updates the site map whenever the server is started const map = require('./utils/generateSiteMap.js'); @@ -50,107 +46,31 @@ app.use(function(request, result) { const filename = url.parse(request.url, true).pathname; + console.log("main main" + filename) var project = false; projects.forEach(function(projectName) { if(filename.startsWith(projectName)) { - require("./includes/projects.js").main(request, result, projectName); + require("./sites/projects.js").main(request, result, projectName); project = true; } }); - - - if(project) + if(!project) { - //don't do blog stuff + require("./sites/blog.js").main(request, result, filename); } - else if(filename.includes(".svg") || filename.includes(".svg")) - { - result.writeHead(200, {'Content-Type': 'image/svg+xml'}); - } - //handles image requests - else if(filename.includes("/img/") || filename.includes(".jpg") || - filename.includes(".png") || filename.includes(".ico")) - { - includes.sendImage(result, filename, cache); - } - //css and font files - else if(filename.includes("/css/") || filename.includes(".woff2") || - filename.includes(".txt")) - { - includes.sendCSS(result, filename, cache); - } - //scripts - else if(filename.includes("/js/") || filename.includes(".js")) - { - includes.sendJS(result, filename, cache); - } - //downloads - else if(filename.includes("/downloads/")) + try { - require("./includes/downloads.js").main(result, filename); - } - else if(filename.includes("/contact")) - { - require("./includes/contact.js").main(request, result); - } - else - { - const html = cache.get(filename); - - result.writeHead(200, {'Content-Type': 'text/html'}); - if(html == null) - { - var file = ""; - - if(filename === '' || filename === '/') - { - file="./posts/homePage.js"; - } - else - { - var urlSplit = filename.split("/"); - - if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page - file = "./posts/category.js"; - - else - file = "./posts/posts.js"; - } - - Promise.all([includes.printHeader(), - require(file).main(filename, request), - includes.printFooter()]).then(function(content) - { - result.write(content.join('')); - result.end(); - cache.put(filename, content.join('')); - - }).catch(function(err) - { - console.log(err); - throw err; - }); - } - else - { - result.write(html); - result.end(); - } + const getClientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0] + || request.connection.remoteAddress; + console.log(getClientAddress); - try - { - const getClientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0] - || request.connection.remoteAddress; - console.log(getClientAddress); - - sql.logTraffic(getClientAddress, filename); - } - catch (e) - { } + sql.logTraffic(getClientAddress, filename); } + catch (e) + { } } else { diff --git a/admin.js b/sites/admin.js similarity index 94% rename from admin.js rename to sites/admin.js index ff986e5..a20c9d5 100644 --- a/admin.js +++ b/sites/admin.js @@ -21,10 +21,10 @@ const app = express(); const session = require('express-session'); //sending static content -const includes = require('./includes/includes.js'); +const includes = require('../includes/includes.js'); //used for file io -const utils = require('./utils/utils.js'); +const utils = require('../utils/utils.js'); //cache -- only used for static contents const cache = require('memory-cache'); diff --git a/sites/blog.js b/sites/blog.js new file mode 100644 index 0000000..b0b20c3 --- /dev/null +++ b/sites/blog.js @@ -0,0 +1,83 @@ +//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'); + + +//caching program to make the application run faster +const cache = require('memory-cache'); + +/** + * @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) + { + if(contentLoader.serveStaticContent(request, result, filename, "")) + { + //do nothing + } + //downloads + else if (filename.includes("/downloads/")) + { + require("../includes/downloads.js").main(result, filename); + } + else if (filename.includes("/contact")) + { + require("../includes/contact.js").main(request, result); + } + else + { + const html = cache.get(filename); + + result.writeHead(200, {'Content-Type': 'text/html'}); + if (html == null) { + var file = ""; + + if (filename === '' || filename === '/') + { + file = "../posts/homePage.js"; + } + else + { + var urlSplit = filename.split("/"); + + if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page + file = "../posts/category.js"; + + else + file = "../posts/posts.js"; + } + + Promise.all([includes.printHeader(), + require(file).main(filename, request), + includes.printFooter()]).then(function (content) + { + result.write(content.join('')); + result.end(); + cache.put(filename, content.join('')); + + }).catch(function (err) + { + console.log(err); + throw err; + }); + } + else + { + result.write(html); + result.end(); + } + } + } + }; \ No newline at end of file diff --git a/sites/projects.js b/sites/projects.js new file mode 100644 index 0000000..0a10dcb --- /dev/null +++ b/sites/projects.js @@ -0,0 +1,42 @@ +//file io +const utils = require('../utils/utils.js'); + +//used to parse the request URL +const url = require('url'); + + +//used to append static content to result +const contentLoader = require('../includes/staticContentServer.js'); + + +/** + * @author Jeffery Russell 10-30-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, baseURL) + { + var filename = url.parse(request.url, true).pathname; + + + if(filename == baseURL || filename == baseURL.substring(0, baseURL.length - 1)) + { + filename = baseURL + "index.html"; + } + console.log("main " + filename) + + + if (!contentLoader.serveStaticContent(request, result, filename, "/blogContent/projects")) + { + //do something? + } + } + }; \ No newline at end of file