From b837b5c98f68ff6c1123d50711a290360fdec54a Mon Sep 17 00:00:00 2001 From: jrtechs Date: Mon, 16 Jul 2018 16:47:38 -0400 Subject: [PATCH] Leveraged both client and server side caching. --- README.md | 2 ++ img/image.js | 22 +++++++++++--- includes/includes.js | 29 ++++++++++++------ package.json | 3 +- server.js | 72 +++++++++++++++++++++++++------------------- 5 files changed, 82 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index de15356..5f89169 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,8 @@ npm install crypto npm install remarkable npm install markdown npm install highlight.js + +npm install memory-cache --save ``` diff --git a/img/image.js b/img/image.js index b2da529..285c238 100644 --- a/img/image.js +++ b/img/image.js @@ -7,13 +7,25 @@ module.exports= * @param result * @param fileName */ - main: function(result, fileName) + main: function(result, fileName, cache) { - result.contentType = 'image/png'; - utils.include("." + fileName).then(function(content) + //result.contentType = 'image/png'; + result.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'max-age=3600'}); + + var img = cache.get(fileName); + if(img == null) + { + utils.include("." + fileName).then(function(content) + { + result.write(content); + result.end(); + cache.put(content); + }); + } + else { - result.write(content); + result.write(img); result.end(); - }); + } } }; \ No newline at end of file diff --git a/includes/includes.js b/includes/includes.js index 822abd0..ece2505 100644 --- a/includes/includes.js +++ b/includes/includes.js @@ -41,17 +41,28 @@ module.exports = * @param path * @return {*} */ - sendCSS: function(result, path) + sendCSS: function(result, path, cache) { - result.writeHead(200, {'Content-Type': 'text/css'}); - utils.include("./" + path).then(function(content) + result.writeHead(200, {'Content-Type': 'text/css', 'Cache-Control': 'max-age=3600'}); + + var css = cache.get(path); + + if(css == null) { - result.write(content); - result.end(); - }).catch(function(error) + utils.include("./" + path).then(function(content) + { + result.write(content); + result.end(); + cache.put(path, content); + }).catch(function(error) + { + console.log(error); + }); + } + else { - console.log(error); - }); - + result.write(css); + result.end(); + } } }; \ No newline at end of file diff --git a/package.json b/package.json index 51af369..735923d 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "highlight": "^0.2.4", "markdown": "^0.5.0", "markdown-to-html": "^0.0.13", + "memory-cache": "^0.2.0", "mysql": "^2.15.0", "promise": "^8.0.1", "sanitizer": "^0.1.3", @@ -31,4 +32,4 @@ "url": "https://github.com/jrtechs/NodeJSBlog/issues" }, "homepage": "https://github.com/jrtechs/NodeJSBlog#readme" -} \ No newline at end of file +} diff --git a/server.js b/server.js index 2530b92..50a907d 100644 --- a/server.js +++ b/server.js @@ -26,7 +26,8 @@ app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxA const port = 8000; -app.use(express.static(__dirname + './', { maxAge: 86400000 })); +const cache = require('memory-cache'); + /** * Parses the request url and calls correct JS files @@ -41,11 +42,11 @@ 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); + require("./img/image.js").main(res, filename, cache); } else if(filename.includes("/css/") || filename.includes(".txt")) { - includes.sendCSS(res, filename) + includes.sendCSS(res, filename, cache) } else if(filename.includes("/downloads/")) { @@ -55,37 +56,48 @@ app.use(function(request, res) { 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 if(urlSplit.length >= 2 && urlSplit[1] === 'admin') //top secret admin page - file = "./admin/admin.js"; + var html = cache.get(filename); + res.writeHead(200, {'Content-Type': 'text/html'}); + if(html == null) + { + if(filename === '' || filename === '/') + { + file="./posts/homePage.js"; + } else - file = "./posts/posts.js"; + { + var urlSplit = filename.split("/"); + + if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page + file = "./posts/category.js"; + + else if(urlSplit.length >= 2 && urlSplit[1] === 'admin') //top secret admin page + file = "./admin/admin.js"; + + else + file = "./posts/posts.js"; + } + + Promise.all([includes.printHeader(), + require(file).main(filename, request), + includes.printFooter()]).then(function(content) + { + res.write(content.join('')); + res.end(); + cache.put(filename, content.join('')); + + }).catch(function(err) + { + console.log(err); + throw err; + }); } - - res.writeHead(200, {'Content-Type': 'text/html'}); - - Promise.all([includes.printHeader(), - require(file).main(filename, request), - includes.printFooter()]).then(function(content) + else { - res.write(content.join('')); + res.write(html); res.end(); - }).catch(function(err) - { - console.log(err); - throw err; - }); + } } } else @@ -93,8 +105,6 @@ app.use(function(request, res) utils.printWrongHost(res); res.end(); } - - }); http.createServer(app).listen(port);