From 8bfb0bfa1e980a8eae682d33ba024972e4ba81be Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 18 Aug 2018 10:16:16 -0400 Subject: [PATCH] refactored includes.js to deal with all static content for the website. --- admin.js | 42 ++++++++------- admin.sh | 7 +++ includes/includes.js | 118 +++++++++++++++++++++++++------------------ js/js.js | 35 ------------- server.js | 71 +++++++++++++++----------- 5 files changed, 141 insertions(+), 132 deletions(-) create mode 100644 admin.sh delete mode 100644 js/js.js diff --git a/admin.js b/admin.js index ece99b5..c81156f 100644 --- a/admin.js +++ b/admin.js @@ -7,33 +7,41 @@ * @author Jeffery Russell 7-21-18 */ +//http server const http = require('http'); +//parsing request url const url = require('url'); +//express app const express = require("express"); +const app = express(); +//session data for login const session = require('express-session'); +//sending static content const includes = require('./includes/includes.js'); -const app = express(); - +//used for file io const utils = require('./utils/utils.js'); -const port = 8001; - +//cache -- only used for static contents const cache = require('memory-cache'); /** Initializes sessions for login */ app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }})); +//port to listen for the admin server on +const port = 8001; + /** * Parses the request url and calls correct JS files */ -app.use(function(request, res) +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")) { @@ -42,28 +50,28 @@ app.use(function(request, res) //handles image requests if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png")) { - require("./img/image.js").main(res, filename, cache); + includes.sendJS(result, filename, cache); } else if(filename.includes("/css/") || filename.includes(".woff2")) { - includes.sendCSS(res, filename, cache) + includes.sendCSS(result, filename, cache) } else if(filename.includes("/js/") || filename.includes(".js")) { - require("./js/js.js").main(res, filename, cache); + includes.sendJS(result, filename, cache); } else { - res.writeHead(200, {'Content-Type': 'text/html'}); + result.writeHead(200, {'Content-Type': 'text/html'}); - file = "./admin/admin.js"; + const file = "./admin/admin.js"; Promise.all([includes.printAdminHeader(), require(file).main(filename, request), includes.printFooter()]).then(function(content) { - res.write(content.join('')); - res.end(); + result.write(content.join('')); + result.end(); }).catch(function(err) { @@ -74,12 +82,10 @@ app.use(function(request, res) } else { - // utils.printWrongHost(res); - res.writeHead(418, {}); - res.end(); + // utils.printWrongHost(result); + result.writeHead(418, {}); + result.end(); } }); -http.createServer(app).listen(port); - - +http.createServer(app).listen(port); \ No newline at end of file diff --git a/admin.sh b/admin.sh new file mode 100644 index 0000000..ca5a09c --- /dev/null +++ b/admin.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# +# Runs the admin nodejs server for jrtechs.net +# +# 8/18/18 Jeffery Russell + +nodejs admin.js \ No newline at end of file diff --git a/includes/includes.js b/includes/includes.js index eb8eb5e..e997480 100644 --- a/includes/includes.js +++ b/includes/includes.js @@ -1,17 +1,70 @@ /** - Includes.js - File used for getting the header and footer + * File: Includes.js + * + * Module used for fetching static content for the website + * like js, css, images, and other static html pages + * + * @author Jeffery Russell */ + +//used for file IO const utils = require('../utils/utils.js'); +//name of header file const HEADER_FILE = "includes/header.html"; +//path of footer file const FOOTER_FILE = "includes/footer.html"; +//admin header path const ADMIN_HEADER = "includes/adminHeader.html"; +//used for hashing stuff for the header's e-tag for clients cache const crypto = require('crypto'); + +/** + * Sends a static file to the client in a way which the web browser + * caches the contents sent. + * + * @param cache -- server's hashmap which reduces file io + * @param path -- file requested by user + * @param type -- type of file for the header + * @param result -- sent to client + */ +const sendCachedContent = function(cache, path, type, result) +{ + const goods = cache.get(path); + + if(goods == null) + { + utils.include("." + path).then(function(content) + { + const eTag = crypto.createHash('md5').update(content).digest('hex'); + result.writeHead(200, {'Content-Type': type, 'Cache-Control': + 'public, max-age=2678400', 'ETag': '"' + eTag + '"', + 'Vary': 'Accept-Encoding'}); + result.write(content); + result.end(); + cache.put(path, content); + }).catch(function(error) + { + console.log(error); + }); + } + else + { + const eTag = crypto.createHash('md5').update(goods).digest('hex'); + result.writeHead(200, {'Content-Type': type, + 'Cache-Control': 'public, max-age=2678400', + 'ETag': '"' + eTag + '"', + 'Vary': 'Accept-Encoding'}); + result.write(goods); + result.end(); + } +}; + + module.exports = { /** Appends the header html section to the result which is @@ -55,34 +108,7 @@ module.exports = */ sendCSS: function(result, path, cache) { - const css = cache.get(path); - - if(css == null) - { - utils.include("./" + path).then(function(content) - { - var eTag = crypto.createHash('md5').update(content).digest('hex'); - result.writeHead(200, {'Content-Type': 'text/css', 'Cache-Control': - 'public, max-age=2678400', 'ETag': '"' + eTag + '"', - 'Vary': 'Accept-Encoding'}); - result.write(content); - result.end(); - cache.put(path, content); - }).catch(function(error) - { - console.log(error); - }); - } - else - { - const eTag = crypto.createHash('md5').update(css).digest('hex'); - result.writeHead(200, {'Content-Type': 'text/css', - 'Cache-Control': 'public, max-age=2678400', - 'ETag': '"' + eTag + '"', - 'Vary': 'Accept-Encoding'}); - result.write(css); - result.end(); - } + sendCachedContent(cache, "/" + path, 'text/css', result); }, @@ -93,25 +119,17 @@ module.exports = */ sendImage: function(result, fileName, cache) { - const img = cache.get(fileName); - if(img == null) - { - utils.include("." + fileName).then(function(content) - { - const eTag = crypto.createHash('md5').update(content).digest('hex'); - console.log(eTag); - result.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"'}); - result.write(content); - result.end(); - cache.put(content); - }); - } - else - { - const eTag = crypto.createHash('md5').update(img).digest('hex'); - result.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"'}); - result.write(img); - result.end(); - } + sendCachedContent(cache, fileName, 'image/png', result); + }, + + + /**Sends the user an image from the specified fileName. + * + * @param result + * @param fileName + */ + sendJS: function(result, fileName, cache) + { + sendCachedContent(cache, fileName, 'application/javascript', result); } }; \ No newline at end of file diff --git a/js/js.js b/js/js.js deleted file mode 100644 index 39b1ed3..0000000 --- a/js/js.js +++ /dev/null @@ -1,35 +0,0 @@ -const utils = require('../utils/utils.js'); -const crypto = require('crypto'); - -module.exports= - { - /**Sends the user an image from the specified fileName. - * - * @param result - * @param fileName - */ - main: function(result, fileName, cache) - { - //result.contentType = 'image/png'; - - var js = cache.get(fileName); - if(js == null) - { - utils.include("." + fileName).then(function(content) - { - var eTag = crypto.createHash('md5').update(content).digest('hex'); - result.writeHead(200, {'Content-Type': 'application/javascript', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"', 'Vary': 'Accept-Encoding'}); - result.write(content); - result.end(); - cache.put(content); - }); - } - else - { - var eTag = crypto.createHash('md5').update(js).digest('hex'); - result.writeHead(200, {'Content-Type': 'application/javascript', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"', 'Vary': 'Accept-Encoding'}); - result.write(js); - result.end(); - } - } - }; \ No newline at end of file diff --git a/server.js b/server.js index f87062d..a463345 100644 --- a/server.js +++ b/server.js @@ -5,72 +5,81 @@ * appropriate pages. */ +//http server const http = require('http'); +//used to parse the request URL const url = require('url'); +//express app const express = require("express"); -//const session = require('express-session'); +//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') +//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'); map.main(); -const app = express(); - -//app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }})); - -//compresses the site - -const compression = require('compression') -app.use(compression()); +//port for the server to run on const port = 8000; -const cache = require('memory-cache'); - /** * Parses the request url and calls correct JS files */ -app.use(function(request, res) +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; //handles image requests - if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png") || filename.includes(".ico")) + if(filename.includes("/img/") || filename.includes(".jpg") || + filename.includes(".png") || filename.includes(".ico")) { - includes.sendImage(res, filename, cache); + includes.sendImage(result, filename, cache); } - else if(filename.includes("/css/") || filename.includes(".woff2") || filename.includes(".txt")) + //css and font files + else if(filename.includes("/css/") || filename.includes(".woff2") || + filename.includes(".txt")) { - includes.sendCSS(res, filename, cache); + includes.sendCSS(result, filename, cache); } + //scripts else if(filename.includes("/js/") || filename.includes(".js")) { - require("./js/js.js").main(res, filename, cache); + includes.sendJS(result, filename, cache); } + //downloads else if(filename.includes("/downloads/")) { - require("./downloads/downloads.js").main(res, filename, request); + require("./downloads/downloads.js").main(result, filename, request); } else { - var file = ""; - const html = cache.get(filename); - res.writeHead(200, {'Content-Type': 'text/html'}); + result.writeHead(200, {'Content-Type': 'text/html'}); if(html == null) { + var file = ""; + if(filename === '' || filename === '/') { file="./posts/homePage.js"; @@ -90,8 +99,8 @@ app.use(function(request, res) require(file).main(filename, request), includes.printFooter()]).then(function(content) { - res.write(content.join('')); - res.end(); + result.write(content.join('')); + result.end(); cache.put(filename, content.join('')); }).catch(function(err) @@ -102,8 +111,8 @@ app.use(function(request, res) } else { - res.write(html); - res.end(); + result.write(html); + result.end(); } try @@ -118,17 +127,21 @@ app.use(function(request, res) { } - } } else { - // utils.printWrongHost(res); - res.writeHead(418, {}); - res.end(); + // utils.printWrongHost(result); + result.writeHead(418, {}); + result.end(); } }); + +//enables gzip compression for the site +app.use(compression()); + + http.createServer(app).listen(port);