Browse Source

Started working on back end server analytics.

pull/4/head
jrtechs 6 years ago
parent
commit
d1a80af430
14 changed files with 71 additions and 45 deletions
  1. +9
    -0
      README.md
  2. +1
    -1
      entries/web-development/node-website-optimization.md
  3. +0
    -2
      includes/includes.js
  4. +0
    -1
      posts/category.js
  5. +0
    -4
      posts/homePage.js
  6. +0
    -1
      posts/posts.js
  7. +16
    -11
      posts/singlePost.js
  8. +17
    -20
      server.js
  9. +0
    -1
      sidebar/categoriesSideBar.js
  10. +0
    -1
      sidebar/popularPosts.js
  11. +0
    -1
      sidebar/sidebar.js
  12. +1
    -0
      sitemap.txt
  13. +0
    -1
      utils/generateSiteMap.js
  14. +27
    -1
      utils/sql.js

+ 9
- 0
README.md View File

@ -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";
```

+ 1
- 1
entries/web-development/node-website-optimization.md View File

@ -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.

+ 0
- 2
includes/includes.js View File

@ -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 =

+ 0
- 1
posts/category.js View File

@ -1,4 +1,3 @@
var Promise = require('promise');
const sql = require('../utils/sql');
const utils = require('../utils/utils.js');

+ 0
- 4
posts/homePage.js View File

@ -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

+ 0
- 1
posts/posts.js View File

@ -1,6 +1,5 @@
const utils = require('../utils/utils.js');
const sql = require('../utils/sql');
const Promise = require('promise');
/**

+ 16
- 11
posts/singlePost.js View File

@ -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
}

+ 17
- 20
server.js View File

@ -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

+ 0
- 1
sidebar/categoriesSideBar.js View File

@ -1,4 +1,3 @@
const Promise = require('promise');
const sql = require('../utils/sql');
module.exports=

+ 0
- 1
sidebar/popularPosts.js View File

@ -1,4 +1,3 @@
const Promise = require('promise');
const sql = require('../utils/sql');
module.exports=

+ 0
- 1
sidebar/sidebar.js View File

@ -1,5 +1,4 @@
const utils = require('../utils/utils.js');
const Promise = require('promise');
module.exports=
{

+ 1
- 0
sitemap.txt View File

@ -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

+ 0
- 1
utils/generateSiteMap.js View File

@ -28,4 +28,3 @@ module.exports=
});
}
}

+ 27
- 1
utils/sql.js View File

@ -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);
}
};

Loading…
Cancel
Save