diff --git a/blog/contact.js b/blog/contact.js index 4527720..c75703b 100644 --- a/blog/contact.js +++ b/blog/contact.js @@ -215,7 +215,6 @@ module.exports = const html = whiskers.render(content[0], templateContext); result.write(html); result.end(); - }).catch(function(err) { console.log(err); diff --git a/includes/includes.js b/includes/includes.js index 68eb7fc..4d2deb8 100644 --- a/includes/includes.js +++ b/includes/includes.js @@ -30,6 +30,25 @@ const crypto = require('crypto'); //caching program to make the application run faster const cache = require('memory-cache'); +const fs = require('fs'); + + +const readFile = function(filename) +{ + return new Promise(function(resolve, reject) + { + try + { + resolve(fs.readFileSync(filename)); + } + catch (e) + { + console.log(e); + console.log("Could not find " + filename); + return(""); + } + }) +}; /** * Sends a static file to the client in a way which the web browser @@ -46,7 +65,7 @@ const sendCachedContent = function(path, type, result) if(goods == null) { - utils.include("." + path).then(function(content) + readFile(path).then(function(content) { const eTag = crypto.createHash('md5').update(content).digest('hex'); result.writeHead(200, {'Content-Type': type, 'Cache-Control': @@ -83,10 +102,48 @@ module.exports = */ printHeader: function(templateContext) { - return utils.includeInObject(HEADER_KEY, templateContext, HEADER_FILE); + return module.exports.includeInObject(HEADER_KEY, templateContext, HEADER_FILE); }, + includeInObject: function(key, context, fileName) + { + return new Promise(function(resolve, reject) + { + readFile(fileName).then(function(result) + { + console.log(result); + context[key] = result; + resolve(); + }).catch(function(error) + { + context[key] = "File Not Found"; + reject(error); + console.log(error); + }) + }); + }, + + + /** + * A function similar to the include statement in PHP + * This function writes a file to the output + * + * @param fileName the file to append to the result + */ + include: function(fileName) + { + try + { + return(fs.readFileSync(fileName)); + } + catch (e) + { + console.log("Could not find " + fileName); + return(""); + } + }, + /** * Appends the footer to the result object * @@ -94,7 +151,7 @@ module.exports = */ printFooter: function(templateContext) { - return utils.includeInObject(FOOTER_KEY, templateContext, FOOTER_FILE); + return module.exports.includeInObject(FOOTER_KEY, templateContext, FOOTER_FILE); }, /** @@ -114,7 +171,7 @@ module.exports = * @param path * @return {*} */ - sendCSS: function(result, path, cache) + sendCSS: function(result, path) { sendCachedContent(path, 'text/css', result); }, @@ -145,7 +202,7 @@ module.exports = fetchTemplate: function(templateName) { - return utils.include("templates/" + templateName); + return readFile("templates/" + templateName); }, @@ -156,7 +213,7 @@ module.exports = */ sendHTML: function(result, fileName) { - utils.include("." + fileName).then(function(content) + readFile("." + fileName).then(function(content) { result.writeHead(200, {'Content-Type': 'text/html'}); result.write(content); diff --git a/routes/about.js b/routes/about.js index 9d95db2..d5ca964 100644 --- a/routes/about.js +++ b/routes/about.js @@ -1,6 +1,6 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +routes.get('/', (request, result) => { }); diff --git a/routes/admin/analytics.js b/routes/admin/analytics.js index 973d8ec..5c68232 100644 --- a/routes/admin/analytics.js +++ b/routes/admin/analytics.js @@ -1,6 +1,6 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +routes.get('/', (request, result) => { }); diff --git a/routes/admin/downloads.js b/routes/admin/downloads.js index 973d8ec..5c68232 100644 --- a/routes/admin/downloads.js +++ b/routes/admin/downloads.js @@ -1,6 +1,6 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +routes.get('/', (request, result) => { }); diff --git a/routes/admin/login.js b/routes/admin/login.js index 973d8ec..5c68232 100644 --- a/routes/admin/login.js +++ b/routes/admin/login.js @@ -1,6 +1,6 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +routes.get('/', (request, result) => { }); diff --git a/routes/admin/posts.js b/routes/admin/posts.js index 973d8ec..5c68232 100644 --- a/routes/admin/posts.js +++ b/routes/admin/posts.js @@ -1,6 +1,6 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +routes.get('/', (request, result) => { }); diff --git a/routes/admin/users.js b/routes/admin/users.js index 973d8ec..5c68232 100644 --- a/routes/admin/users.js +++ b/routes/admin/users.js @@ -1,6 +1,6 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +routes.get('/', (request, result) => { }); diff --git a/routes/category/index.js b/routes/category/index.js index 973d8ec..5c68232 100644 --- a/routes/category/index.js +++ b/routes/category/index.js @@ -1,6 +1,6 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +routes.get('/', (request, result) => { }); diff --git a/routes/contact.js b/routes/contact.js index b6d1ffc..4c81fae 100644 --- a/routes/contact.js +++ b/routes/contact.js @@ -1,8 +1,8 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +routes.get('/', (request, result) => { - + require("../blog/contact.js").main(request, result); }); routes.get('*', (request, result) => diff --git a/routes/downloads/index.js b/routes/downloads/index.js index 887044f..01ec4f1 100644 --- a/routes/downloads/index.js +++ b/routes/downloads/index.js @@ -1,8 +1,12 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => -{ +/** used to parse the request URL */ +const url = require('url'); +routes.get('/', (request, result) => +{ + const filename = url.parse(request.url, true).pathname; + require("../../includes/downloads.js").main(result, filename); }); diff --git a/routes/includes/index.js b/routes/includes/index.js index 887044f..70a5ddf 100644 --- a/routes/includes/index.js +++ b/routes/includes/index.js @@ -1,8 +1,20 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +/** used to parse the request URL */ +const url = require('url'); + +const contentLoader = require('../../includes/staticContentServer.js'); + + + +routes.get('*', (request, result) => { + var filename = url.parse(request.url, true).pathname; + if(!contentLoader.serveStaticContent(request, result, filename, "includes")) + { + //missing content + } }); diff --git a/routes/index.js b/routes/index.js index 695e6c4..a30cbad 100644 --- a/routes/index.js +++ b/routes/index.js @@ -6,12 +6,17 @@ routes.use('/about', about); const contact = require('./contact'); routes.use('/contact', contact); +const includes = require('./includes'); +routes.use('/includes', includes); + const projects = ["/steam/"]; + + routes.get('/', (request, result) => { - //blog home + //blog home page }); diff --git a/routes/projects/index.js b/routes/projects/index.js index 973d8ec..5c68232 100644 --- a/routes/projects/index.js +++ b/routes/projects/index.js @@ -1,6 +1,6 @@ const routes = require('express').Router(); -routes.post('/', (request, result) => +routes.get('/', (request, result) => { }); diff --git a/sites/blog.js b/sites/blog.js index 1c1b142..3d798ae 100644 --- a/sites/blog.js +++ b/sites/blog.js @@ -43,10 +43,6 @@ module.exports= { require("../includes/downloads.js").main(result, filename); } - else if (filename.includes("/contact")) - { - require("../blog/contact.js").main(request, result); - } else { var page = request.query.page; diff --git a/utils/utils.js b/utils/utils.js index 376bc89..534d7a9 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -6,47 +6,12 @@ //used for file io const fs = require('fs'); +const includes = require("../includes/includes"); + module.exports= { - /** - * A function similar to the include statement in PHP - * This function writes a file to the output - * - * @param fileName the file to append to the result - */ - include: function(fileName) - { - return new Promise(function(resolve, reject) - { - try - { - resolve(fs.readFileSync(fileName)); - } - catch (e) - { - console.log("Could not find " + fileName); - resolve(""); - } - }); - }, - includeInObject: function(key, context, fileName) - { - return new Promise(function(resolve, reject) - { - module.exports.include(fileName).then(function(result) - { - context[key] = result; - resolve(); - }).catch(function(error) - { - context[key] = "File Not Found"; - reject(error); - console.log(error); - }) - }) - }, /**