Browse Source

Updated the homepage to use better async methods for performance.

pull/4/head
jrtechs 6 years ago
parent
commit
4534e5534d
16 changed files with 143 additions and 102 deletions
  1. BIN
      entries/programs/media/90e4464eb85c998c8db42c965468aa54.png
  2. BIN
      entries/projects/media/088cc48e754c1b99e0fcd5a5eddb9d64.png
  3. BIN
      entries/projects/media/161edc628257e8a2f92086c5987dcf0f.png
  4. BIN
      entries/projects/media/fc0a0ddf2a914b171bb3a5013e9e7b6c.png
  5. BIN
      entries/projects/media/steam/ben2.png
  6. BIN
      entries/projects/media/steam/diagram.png
  7. BIN
      entries/projects/media/steam/jrtechs2.png
  8. +5
    -2
      img/image.js
  9. +21
    -16
      includes/includes.js
  10. +26
    -16
      posts/homePage.js
  11. +5
    -9
      posts/singlePost.js
  12. +26
    -11
      server.js
  13. +12
    -15
      sidebar/categoriesSideBar.js
  14. +15
    -12
      sidebar/recentPosts.js
  15. +29
    -17
      sidebar/sidebar.js
  16. +4
    -4
      utils/utils.js

BIN
entries/programs/media/90e4464eb85c998c8db42c965468aa54.png View File

Before After
Width: 1429  |  Height: 678  |  Size: 204 KiB Width: 1429  |  Height: 678  |  Size: 176 KiB

BIN
entries/projects/media/088cc48e754c1b99e0fcd5a5eddb9d64.png View File

Before After
Width: 367  |  Height: 726  |  Size: 27 KiB Width: 367  |  Height: 726  |  Size: 5.8 KiB

BIN
entries/projects/media/161edc628257e8a2f92086c5987dcf0f.png View File

Before After
Width: 2217  |  Height: 1105  |  Size: 123 KiB Width: 2217  |  Height: 1105  |  Size: 77 KiB

BIN
entries/projects/media/fc0a0ddf2a914b171bb3a5013e9e7b6c.png View File

Before After
Width: 1024  |  Height: 595  |  Size: 26 KiB Width: 1024  |  Height: 595  |  Size: 20 KiB

BIN
entries/projects/media/steam/ben2.png View File

Before After
Width: 1909  |  Height: 941  |  Size: 489 KiB Width: 1909  |  Height: 941  |  Size: 330 KiB

BIN
entries/projects/media/steam/diagram.png View File

Before After
Width: 3238  |  Height: 1856  |  Size: 73 KiB Width: 3238  |  Height: 1856  |  Size: 59 KiB

BIN
entries/projects/media/steam/jrtechs2.png View File

Before After
Width: 1894  |  Height: 940  |  Size: 455 KiB Width: 1894  |  Height: 940  |  Size: 294 KiB

+ 5
- 2
img/image.js View File

@ -10,7 +10,10 @@ module.exports=
main: function(result, fileName) main: function(result, fileName)
{ {
result.contentType = 'image/png'; result.contentType = 'image/png';
utils.include(result, "." + fileName);
result.end();
utils.include("." + fileName).then(function(content)
{
result.write(content);
result.end();
});
} }
}; };

+ 21
- 16
includes/includes.js View File

@ -18,30 +18,28 @@ module.exports =
* @param result * @param result
* @return {*} a promise retrieved from the utils.include function * @return {*} a promise retrieved from the utils.include function
*/ */
printHeader: function(result)
printHeader: function()
{ {
result.writeHead(200, {'Content-Type': 'text/html'});
return utils.include(result, HEADER_FILE);
// utils.getFileContents(HEADER_FILE);
return utils.include(HEADER_FILE);
}, },
/** /**
* Appends the footer to the result object * Appends the footer to the result object
* *
* @param result
* @return {*|Promise} * @return {*|Promise}
*/ */
printFooter: function(result)
printFooter: function()
{ {
return new Promise(function(resolve, reject)
{
utils.include(result, FOOTER_FILE).then(function()
{
result.end();
resolve();
})
})
return utils.include(FOOTER_FILE);
// return new Promise(function(resolve, reject)
// {
// resolve(utils.getFileContents(FOOTER_FILE));
// })
}, },
/**Sends a css file to the user /**Sends a css file to the user
* *
* @param result * @param result
@ -51,7 +49,14 @@ module.exports =
sendCSS: function(result, path) sendCSS: function(result, path)
{ {
result.writeHead(200, {'Content-Type': 'text/css'}); result.writeHead(200, {'Content-Type': 'text/css'});
utils.include(result, "./" + path);
result.end();
utils.include("./" + path).then(function(content)
{
result.write(content);
result.end();
}).catch(function(error)
{
console.log(error);
});
} }
}; };

+ 26
- 16
posts/homePage.js View File

@ -11,21 +11,22 @@ const postRenderer = require('../posts/singlePost.js');
* @param result * @param result
* @returns {*|Promise} * @returns {*|Promise}
*/ */
var renderRecentPosts = function(result)
var renderRecentPosts = function()
{ {
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
console.log("recent post render page");
sql.getRecentPostSQL().then(function(posts) sql.getRecentPostSQL().then(function(posts)
{ {
var postPromises = []; var postPromises = [];
result.write("<div class='col-md-8'>");
var content = "<div class='col-md-8'>";
posts.forEach(function(post) posts.forEach(function(post)
{ {
postPromises.push(new Promise(function(res, rej) postPromises.push(new Promise(function(res, rej)
{ {
postRenderer.renderPreview(result, post).then(function()
postRenderer.renderPreview(post).then(function(cont)
{ {
res();
res(cont);
}).catch(function(error) }).catch(function(error)
{ {
rej(error); rej(error);
@ -33,17 +34,16 @@ var renderRecentPosts = function(result)
})); }));
}); });
Promise.all(postPromises).then(function()
Promise.all(postPromises).then(function(cont)
{ {
result.write("</div>");
resolve();
content = content + cont.join('') + "</div>";
resolve(content);
}).catch(function(error) }).catch(function(error)
{ {
reject(error); reject(error);
}) })
}).catch(function(error) }).catch(function(error)
{ {
console.log(error);
reject(error); reject(error);
}) })
}); });
@ -57,22 +57,32 @@ module.exports=
* @param res * @param res
* @param fileName request url * @param fileName request url
*/ */
main: function(result, requestURL, request)
main: function(requestURL, request)
{ {
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
// renderRecentPosts().then(function()
// {
// return require("../sidebar/sidebar.js").main();
// }).then(function()
// {
// resolve();
// }).catch(function(error)
// {
// console.log(error);
// reject(error);
// })
renderRecentPosts(result).then(function()
{
return require("../sidebar/sidebar.js").main(result);
}).then(function()
console.log("home page");
Promise.all([renderRecentPosts(), require("../sidebar/sidebar.js").main()]).then(function(content)
{ {
resolve();
console.log("fin posts");
resolve(content.join(''));
}).catch(function(error) }).catch(function(error)
{ {
console.log(error);
reject(error); reject(error);
})
});
}) })
} }
}; };

+ 5
- 9
posts/singlePost.js View File

@ -36,7 +36,7 @@ module.exports=
* @param res * @param res
* @param post * @param post
*/ */
renderPreview: function(res, post)
renderPreview: function(post)
{ {
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
@ -88,9 +88,8 @@ module.exports=
" </div>\n"; " </div>\n";
html += "</div></div></div><br><br>"; html += "</div></div></div><br><br>";
res.write(html);
resolve()
resolve(html)
}).catch(function(error) }).catch(function(error)
{ {
console.log(error); console.log(error);
@ -100,7 +99,6 @@ module.exports=
catch(ex) catch(ex)
{ {
reject(ex); reject(ex);
console.log(ex);
} }
}); });
}, },
@ -112,7 +110,7 @@ module.exports=
* @param post sql data about the blog post * @param post sql data about the blog post
* @return {*|Promise} * @return {*|Promise}
*/ */
renderPost: function(res, post)
renderPost: function(post)
{ {
return new Promise(function (resolve, reject) return new Promise(function (resolve, reject)
{ {
@ -148,14 +146,12 @@ module.exports=
html += "</div></div></div><br><br>"; html += "</div></div></div><br><br>";
res.write(html);
resolve()
resolve(html);
}); });
} }
catch(ex) catch(ex)
{ {
//console.log(ex);
//utils.include(res, "includes/404.html");
reject(ex);
} }
}); });
} }

+ 26
- 11
server.js View File

@ -9,22 +9,22 @@ const http = require('http');
const url = require('url'); const url = require('url');
var express = require("express");
const express = require("express");
var session = require('express-session');
const session = require('express-session');
const includes = require('./includes/includes.js'); const includes = require('./includes/includes.js');
const utils = require('./utils/utils.js'); const utils = require('./utils/utils.js');
var map = require('./utils/generateSiteMap.js');
const map = require('./utils/generateSiteMap.js');
map.main(); map.main();
var app = express();
const app = express();
app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }})); app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }}));
var port = 8000;
const port = 8000;
/** /**
@ -76,16 +76,31 @@ app.use(function(request, res)
file = "./posts/posts.js"; file = "./posts/posts.js";
} }
includes.printHeader(res).then(function()
// includes.printHeader(res).then(function()
// {
// return require(file).main(res, filename, request);
// }).then(function()
// {
// return includes.printFooter(res);
// }).catch(function(err)
// {
// console.log(err);
// })
res.writeHead(200, {'Content-Type': 'text/html'});
Promise.all([includes.printHeader(),
require(file).main(filename, request),
includes.printFooter()]).then(function(content)
{ {
return require(file).main(res, filename, request);
}).then(function()
{
return includes.printFooter(res);
console.log("main fin");
res.write(content.join(''));
res.end();
}).catch(function(err) }).catch(function(err)
{ {
console.log(err); console.log(err);
})
});
} }
} }
else else

+ 12
- 15
sidebar/categoriesSideBar.js View File

@ -10,33 +10,30 @@ module.exports=
* @param res * @param res
* @return {*|Promise} * @return {*|Promise}
*/ */
main: function(res)
main: function()
{ {
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
res.write("<br><br><div class=\"container\">");
var content = "<br><br><div class=\"container\">";
//res.write("<div class=\"w3-container w3-padding w3-gray\"><h4>Categories</h4></div>");
content += "<div class=\"list-group\">";
//res.write("<div class=\"container\"><h4 class=\"align-content-center\">Categories</h4></div>");
//res.write("<div class=\"w3-bar-block w3-white\">");
res.write("<div class=\"list-group\">");
res.write(" <a href=\"#\" class=\"list-group-item list-group-item-action flex-column align-items-start active\">\n" +
content += " <a href=\"#\" class=\"list-group-item list-group-item-action flex-column align-items-start active\">\n" +
" <h5 class=\"mb-1\">Categories</h5>\n" + " <h5 class=\"mb-1\">Categories</h5>\n" +
" </a>");
" </a>";
sql.getCategories().then(function(categories) sql.getCategories().then(function(categories)
{ {
categories.forEach(function(cat) categories.forEach(function(cat)
{ {
//res.write(cat.name);
res.write("<a class=\"list-group-item\" href='/category/" + cat.url + "'>" + cat.name + "<br></a>");
content += "<a class=\"list-group-item\" href='/category/" + cat.url + "'>" + cat.name + "<br></a>";
}); });
res.write("</div></div><br>");
resolve();
})
content += "</div></div><br>";
resolve(content);
}).catch(function(error)
{
reject(error);
});
}); });
} }
}; };

+ 15
- 12
sidebar/recentPosts.js View File

@ -5,35 +5,38 @@ module.exports=
{ {
/** Renders the the recent post sidebar. /** Renders the the recent post sidebar.
* *
* @param res
* @returns {*|Promise} * @returns {*|Promise}
*/ */
main: function(res)
main: function()
{ {
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
res.write("<div class=\"container\">");
console.log("recent page");
// res.write("<div class=\"\">" +
// "<h4 class=\"text-center\">Recent Posts</h4></div>");
res.write("<div class=\"list-group\">");
var content = "<div class=\"container\">";
res.write(" <a href=\"#\" class=\"list-group-item list-group-item-action flex-column align-items-start active\">\n" +
content +="<div class=\"list-group\">";
content +=" <a href=\"#\" class=\"list-group-item list-group-item-action flex-column align-items-start active\">\n" +
" <h5 class=\"mb-1\">Recent Posts</h5>\n" + " <h5 class=\"mb-1\">Recent Posts</h5>\n" +
" </a>");
" </a>";
sql.getRecentPosts().then(function(posts) sql.getRecentPosts().then(function(posts)
{ {
posts.forEach(function(p) posts.forEach(function(p)
{ {
var url = '/' + p.category + '/' + p.url; var url = '/' + p.category + '/' + p.url;
res.write("<a class=\"list-group-item\" href='"
+ url + "'>" + p.name + "<br></a>");
content += "<a class=\"list-group-item\" href='"
+ url + "'>" + p.name + "<br></a>";
}); });
res.write("</div></div>");
resolve();
content +="</div></div>";
resolve(content);
}).catch(function(error)
{
reject(error);
}) })
}); });
} }

+ 29
- 17
sidebar/sidebar.js View File

@ -1,5 +1,5 @@
const utils = require('../utils/utils.js'); const utils = require('../utils/utils.js');
var Promise = require('promise');
const Promise = require('promise');
module.exports= module.exports=
{ {
@ -9,28 +9,40 @@ module.exports=
* @param res * @param res
* @returns {*|Promise} * @returns {*|Promise}
*/ */
main: function(res)
main: function()
{ {
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
res.write("<div class=\"col-md-4\">");
console.log("sidebar page");
utils.include(res,"sidebar/sidebar.html").then(function()
{
return require("../sidebar/recentPosts.js").main(res);
}).then(function()
{
return require("../sidebar/categoriesSideBar.js")
.main(res);
}).then(function()
// res.write("<div class=\"col-md-4\">");
//
// utils.include(res,"sidebar/sidebar.html").then(function()
// {
// return require("../sidebar/recentPosts.js").main(res);
// }).then(function()
// {
// return require("../sidebar/categoriesSideBar.js")
// .main(res);
// }).then(function()
// {
// res.write("</div>");
// resolve();
// }).catch(function(err)
// {
// console.log("hit error");
// console.log(err);
// })
Promise.all([utils.include("sidebar/sidebar.html"),
require("../sidebar/recentPosts.js").main(),
require("../sidebar/categoriesSideBar.js").main()]).then(function(content)
{ {
res.write("</div>");
resolve();
}).catch(function(err)
resolve("<div class=\"col-md-4\">" + content.join('') + "</div>");
}).catch(function(error)
{ {
console.log("hit error");
console.log(err);
})
reject(error);
});
}) })
} }
}; };

+ 4
- 4
utils/utils.js View File

@ -13,22 +13,22 @@ module.exports=
* A function similar to the include statement in PHP * A function similar to the include statement in PHP
* This function writes a file to the output * This function writes a file to the output
* *
* @param result the result that is sent to the user from node
* @param fileName the file to append to the result * @param fileName the file to append to the result
*/ */
include: function(result, fileName)
include: function(fileName)
{ {
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
try try
{ {
result.write(fs.readFileSync(fileName));
resolve(fs.readFileSync(fileName));
} }
catch (e) catch (e)
{ {
console.log("Could not find " + fileName); console.log("Could not find " + fileName);
resolve("");
} }
resolve();
}); });
}, },

Loading…
Cancel
Save