Browse Source

Got blog posts working with the routes system.

pull/61/head
jrtechs 5 years ago
parent
commit
a5a51bf1e1
5 changed files with 21 additions and 146 deletions
  1. +1
    -1
      blog/category.js
  2. +1
    -1
      blog/renderBlogPost.js
  3. +6
    -7
      routes/index.js
  4. +0
    -108
      sites/blog.js
  5. +13
    -29
      utils/pageBuilder.js

+ 1
- 1
blog/category.js View File

@ -26,7 +26,7 @@ module.exports=
sql.getPostsFromCategory(category).then(function(posts) sql.getPostsFromCategory(category).then(function(posts)
{ {
Promise.all([blogBodyRenderer.renderBatchOfPosts(requestURL, posts, page, 5, templateContext), Promise.all([blogBodyRenderer.renderBatchOfPosts(requestURL, posts, page, 5, templateContext),
require('./renderNextBar').main(requestURL, page, 5, posts.length, templateContext)]).then(function()
require('./renderNextBar').main("/category" + request.url, page, 5, posts.length, templateContext)]).then(function()
{ {
resolve(); resolve();
}); });

+ 1
- 1
blog/renderBlogPost.js View File

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

+ 6
- 7
routes/index.js View File

@ -25,23 +25,22 @@ routes.use('/includes', includes);
const category = require('./category'); const category = require('./category');
routes.use('/category', category); routes.use('/category', category);
const pageBuilder = require('../utils/pageBuilder');
const projects = ["/steam/"]; const projects = ["/steam/"];
//blog home page
routes.get('/', (request, result) => routes.get('/', (request, result) =>
{ {
//blog home page
pageBuilder.buildBlogPage(request, result, require("../blog/homePage").main)
}); });
//regular blog posts
routes.get('*', (request, result) => routes.get('*', (request, result) =>
{ {
console.log("Un registered event.")
pageBuilder.buildBlogPage(request, result,
require("../blog/posts").main, request.url);
}); });
module.exports = routes; module.exports = routes;

+ 0
- 108
sites/blog.js View File

@ -1,108 +0,0 @@
//used to append static content to result
const includes = require('../includes/includes.js');
//used to append static content to result
const contentLoader = require('../includes/staticContentServer.js');
const whiskers = require('whiskers');
const TEMPLATE_FILE="blog/blogMain.html";
const PAGINATION_TEMPLATE_KEY = "paginationTemplate";
const PAGINATION_TEMPLATE_FILE = "blog/paginationBar.html";
//caching program to make the application run faster
const cache = require('memory-cache');
//file io
const utils = require('../utils/utils.js');
/**
* @author Jeffery Russell 11-3-18
*
* @type {{main: (function(*=, *): Promise)}}
*/
module.exports=
{
/**
* Calls posts and sidebar modules to render blog contents in order
*
* @param requestURL
* @returns {Promise|*}
*/
main: function(request, result, filename)
{
var page = request.query.page;
if(typeof page == "undefined")
page = 1;
page = Number(page);
const html = cache.get(filename + "?page=" + page);
result.writeHead(200, {'Content-Type': 'text/html'});
if (html == null)
{
var file = "";
if (filename === '' || filename === '/')
{
file = "../blog/homePage.js";
}
else
{
var urlSplit = filename.split("/");
if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
file = "../blog/category.js";
else
{
file = "../blog/posts.js";
page = 1; // all blog are single page, everyone must be one to ensure
// cache is not tricked into storing same blog post a ton of times
}
}
var templateContext = Object();
Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
utils.includeInObject(PAGINATION_TEMPLATE_KEY, templateContext, "templates/" + PAGINATION_TEMPLATE_FILE),
includes.printHeader(templateContext),
includes.printFooter(templateContext),
require(file).main(filename, request, templateContext),
require("../blog/sidebar.js").main(templateContext)])
.then(function (content)
{
const html = whiskers.render(content[0], templateContext);
result.write(html);
result.end();
cache.put(filename + "?page=" + page, html);
}).catch(function (err)
{
console.log(err);
cache.del(filename + "?page=" + page);
utils.print404().then(function(content)
{
result.write(content);
result.end();
})
});
}
else
{
result.write(html);
result.end();
}
},
/**
* Clears the memory cache.
*/
clearCache: function()
{
console.log("Blog cache cleared");
cache.clear();
}
};

+ 13
- 29
utils/pageBuilder.js View File

@ -128,40 +128,18 @@ module.exports =
}, },
buildBlogPage: function(request, result, templateFiller)
buildBlogPageWithURL: function(request, result, templateFiller, filename)
{ {
var page = request.query.page; var page = request.query.page;
if(typeof page == "undefined") if(typeof page == "undefined")
page = 1; page = 1;
page = Number(page); page = Number(page);
var filename = url.parse(request.url, true).pathname;
const html = cache.get(filename + "?page=" + page); const html = cache.get(filename + "?page=" + page);
result.writeHead(200, {'Content-Type': 'text/html'}); result.writeHead(200, {'Content-Type': 'text/html'});
if (html == null) if (html == null)
{ {
// var file = "";
//
// if (filename === '' || filename === '/')
// {
// file = "../blog/homePage.js";
// }
// else
// {
// var urlSplit = filename.split("/");
//
// if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
// file = "../blog/category.js";
// else
// {
// file = "../blog/posts.js";
// page = 1; // all blog are single page, everyone must be one to ensure
// // cache is not tricked into storing same blog post a ton of times
// }
// }
var templateContext = Object(); var templateContext = Object();
Promise.all([includes.fetchTemplate(TEMPLATE_FILE), Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
includes.includeInObject(PAGINATION_TEMPLATE_KEY, templateContext, "templates/" + PAGINATION_TEMPLATE_FILE), includes.includeInObject(PAGINATION_TEMPLATE_KEY, templateContext, "templates/" + PAGINATION_TEMPLATE_FILE),
@ -177,13 +155,13 @@ module.exports =
result.end(); result.end();
cache.put(filename + "?page=" + page, html); cache.put(filename + "?page=" + page, html);
}).catch(function (err) }).catch(function (err)
{
console.log("dewie");
console.log(err);
cache.del(filename + "?page=" + page);
{
console.log("dewie");
console.log(err);
cache.del(filename + "?page=" + page);
module.exports.print404(result)
});
module.exports.print404(result)
});
} }
else else
{ {
@ -192,6 +170,12 @@ module.exports =
} }
}, },
buildBlogPage: function(request, result, templateFiller)
{
var filename = url.parse(request.url, true).pathname;
module.exports.buildBlogPageWithURL(request, result,templateFiller, filename);
},
/** /**
* Clears the memory cache. * Clears the memory cache.
*/ */

Loading…
Cancel
Save