From d1a80af4301d7b40729391b6c749cc8001909ab6 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sun, 29 Jul 2018 18:54:28 -0400 Subject: [PATCH] Started working on back end server analytics. --- README.md | 9 +++++ .../node-website-optimization.md | 2 +- includes/includes.js | 2 - posts/category.js | 1 - posts/homePage.js | 4 -- posts/posts.js | 1 - posts/singlePost.js | 27 ++++++++------ server.js | 37 +++++++++---------- sidebar/categoriesSideBar.js | 1 - sidebar/popularPosts.js | 1 - sidebar/sidebar.js | 1 - sitemap.txt | 1 + utils/generateSiteMap.js | 1 - utils/sql.js | 28 +++++++++++++- 14 files changed, 71 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index a17ca88..e846341 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,15 @@ post_id mediumint unsigned not null, primary key(popular_post_id) ); +create table traffic_log( +log_id mediumint unsigned not null AUTO_INCREMENT, +url varchar(60) not null, +ip varchar(20) not null, +date datetime not null, +primary key(log_id) +); + + grant all on jrtechs_blog.* to blog_user@localhost identified by "password"; ``` diff --git a/entries/web-development/node-website-optimization.md b/entries/web-development/node-website-optimization.md index 43c8d29..9416a01 100644 --- a/entries/web-development/node-website-optimization.md +++ b/entries/web-development/node-website-optimization.md @@ -11,7 +11,7 @@ I did to decrease the load time of this blog written in node by two seconds. This is the result for a single blog post. -Before the improvements my home page took 3.14 seconds to load and was 3mb. Now +Before the improvements, my home page took 3.14 seconds to load and was 3mb. Now my home page takes 1.22 seconds to load and is only 1.2mb in size. If you look at the waterfall for my home page, most of the time is a result of the youtube embedded videos loading. diff --git a/includes/includes.js b/includes/includes.js index 0b8cf06..9bcc7e0 100644 --- a/includes/includes.js +++ b/includes/includes.js @@ -10,8 +10,6 @@ const FOOTER_FILE = "includes/footer.html"; const ADMIN_HEADER = "includes/adminHeader.html"; -const Promise = require('promise'); - const crypto = require('crypto'); module.exports = diff --git a/posts/category.js b/posts/category.js index 1e4e72e..3f1ddcd 100644 --- a/posts/category.js +++ b/posts/category.js @@ -1,4 +1,3 @@ -var Promise = require('promise'); const sql = require('../utils/sql'); const utils = require('../utils/utils.js'); diff --git a/posts/homePage.js b/posts/homePage.js index 7688c9b..7da33e8 100644 --- a/posts/homePage.js +++ b/posts/homePage.js @@ -1,9 +1,5 @@ const sql = require('../utils/sql'); -const utils = require('../utils/utils.js'); - -const Promise = require('promise'); - const postRenderer = require('../posts/singlePost.js'); /**Renders each recent post for the homepage of the website diff --git a/posts/posts.js b/posts/posts.js index b27293f..2607be7 100644 --- a/posts/posts.js +++ b/posts/posts.js @@ -1,6 +1,5 @@ const utils = require('../utils/utils.js'); const sql = require('../utils/sql'); -const Promise = require('promise'); /** diff --git a/posts/singlePost.js b/posts/singlePost.js index 233393e..5a74004 100644 --- a/posts/singlePost.js +++ b/posts/singlePost.js @@ -1,27 +1,32 @@ const utils = require('../utils/utils.js'); -var Promise = require('promise'); - -var markdown = require( "markdown" ).markdown; +const markdown = require( "markdown" ).markdown; const sql = require('../utils/sql'); var Remarkable = require('remarkable'); -var hljs = require('highlight.js') // https://highlightjs.org/ +var hljs = require('highlight.js'); // Actual default values -var md = new Remarkable({ +var md = new Remarkable( +{ html: true, - highlight: function (str, lang) { - if (lang && hljs.getLanguage(lang)) { - try { + highlight: function (str, lang) + { + if (lang && hljs.getLanguage(lang)) + { + try + { return hljs.highlight(lang, str).value; - } catch (err) {} + } + catch (err) {} } - try { + try + { return hljs.highlightAuto(str).value; - } catch (err) {} + } + catch (err) {} return ''; // use external default escaping } diff --git a/server.js b/server.js index 7479333..28fb0ff 100644 --- a/server.js +++ b/server.js @@ -15,6 +15,8 @@ const express = require("express"); const includes = require('./includes/includes.js'); +const sql = require('./utils/sql'); + const map = require('./utils/generateSiteMap.js'); map.main(); @@ -44,7 +46,7 @@ app.use(function(request, res) const filename = url.parse(request.url, true).pathname; //handles image requests - if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png")) + if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png") || filename.includes(".ico")) { require("./img/image.js").main(res, filename, cache); } @@ -60,25 +62,6 @@ app.use(function(request, res) { require("./downloads/downloads.js").main(res, filename, request); } - else if(filename.includes("/admin")) - { - res.writeHead(200, {'Content-Type': 'text/html'}); - - file = "./admin/admin.js"; - - Promise.all([includes.printHeader(), - require(file).main(filename, request), - includes.printFooter()]).then(function(content) - { - res.write(content.join('')); - res.end(); - - }).catch(function(err) - { - console.log(err); - throw err; - }); - } else { var file = ""; @@ -122,6 +105,20 @@ app.use(function(request, res) res.write(html); res.end(); } + + try + { + var ip = (request.headers['x-forwarded-for'] || '').split(',').pop() || + request.connection.remoteAddress || + request.socket.remoteAddress || + request.connection.socket.remoteAddress; + sql.logTraffic(ip, filename); + } + catch (e) + { + + } + } } else diff --git a/sidebar/categoriesSideBar.js b/sidebar/categoriesSideBar.js index 8ee3968..0429960 100644 --- a/sidebar/categoriesSideBar.js +++ b/sidebar/categoriesSideBar.js @@ -1,4 +1,3 @@ -const Promise = require('promise'); const sql = require('../utils/sql'); module.exports= diff --git a/sidebar/popularPosts.js b/sidebar/popularPosts.js index da4b929..be465a1 100644 --- a/sidebar/popularPosts.js +++ b/sidebar/popularPosts.js @@ -1,4 +1,3 @@ -const Promise = require('promise'); const sql = require('../utils/sql'); module.exports= diff --git a/sidebar/sidebar.js b/sidebar/sidebar.js index c301a2f..b5e6b94 100644 --- a/sidebar/sidebar.js +++ b/sidebar/sidebar.js @@ -1,5 +1,4 @@ const utils = require('../utils/utils.js'); -const Promise = require('promise'); module.exports= { diff --git a/sitemap.txt b/sitemap.txt index 5dd17f6..e82144a 100644 --- a/sitemap.txt +++ b/sitemap.txt @@ -13,6 +13,7 @@ http://jrtechs.net/projects/java-fibonacci-solver http://jrtechs.net/projects/musical-floppy-drive-build-log http://jrtechs.net/java/gremlin-in-10-minutes http://jrtechs.net/java/top-three-recommended-java-ides +http://jrtechs.net/other/my-college-essay http://jrtechs.net/web-development/node-website-optimization http://jrtechs.net/web-development/node-website-optimization http://jrtechs.net/programming/gremlin-in-10-minutes diff --git a/utils/generateSiteMap.js b/utils/generateSiteMap.js index 41ed113..f65be2b 100644 --- a/utils/generateSiteMap.js +++ b/utils/generateSiteMap.js @@ -28,4 +28,3 @@ module.exports= }); } } - diff --git a/utils/sql.js b/utils/sql.js index b1d096f..a558c57 100644 --- a/utils/sql.js +++ b/utils/sql.js @@ -65,7 +65,7 @@ module.exports= if (err) { console.log(err); - resolve(0); + reject(); } resolve(result.insertId); }); @@ -465,5 +465,31 @@ module.exports= }); }); + }, + + + /** + * Logs visited page for backend server analytics. + * + * @param ip + * @param page + */ + logTraffic: function(ip, page) + { + if(page.length > 40) + { + console.log("Error, request too long to log ip:" + + ip + " page: " + page); + return; + } + + if(ip.length > 20) + { + ip = ""; + } + + const q = "insert into traffic_log (url, ip, date) values " + + "('" + page + "', '" + ip + "', now())"; + module.exports.insert(q); } }; \ No newline at end of file