From 3e6b0a780840384498bf98ef55e890b64e05b8bd Mon Sep 17 00:00:00 2001 From: jrtechs Date: Tue, 23 Jan 2018 21:37:40 -0500 Subject: [PATCH] Worked on the categories sidebar and cleaning up promise statements. --- admin/admin.js | 58 ++++++++------------------------ img/image.js | 15 +++++---- includes/includes.js | 36 +++++++++++++++----- posts/category.js | 12 +------ server.js | 65 ++++++------------------------------ sidebar/categoriesSideBar.js | 63 +++++++++++++--------------------- utils/sql.js | 1 - utils/utils.js | 45 +++++++++++++++---------- 8 files changed, 113 insertions(+), 182 deletions(-) diff --git a/admin/admin.js b/admin/admin.js index 9c901b8..da72ba4 100644 --- a/admin/admin.js +++ b/admin/admin.js @@ -1,58 +1,26 @@ const utils = require('../utils/utils.js'); var Promise = require('promise'); - - module.exports= { + /** + * Method calls the admin widgets it correct order + * + * @param result + * @param fileName + * @param request + * @return {*|Promise} + */ main: function(result, fileName, request) { - return new Promise(function(resolve, reject){ - var promiseToGetPost = function(req) - { - return new Promise(function(resolve, reject) - { - if(req.method == 'POST') - { - var body = ''; - - req.on('data', function (data) - { - body += data; - - //Kills request, don't steal my RAM!! - //You can only download so much ram ;) - if (body.length > 1e6) - { - req.connection.destroy(); - reject(); - } - - }); - - req.on('end', function () - { - console.log(body); - resolve(body); - }); - } - else - { - resolve(0); - } - }); - - }; - var promiseToDisplayContents = function(postData) - { - return require("../admin/addCategory.js").main(result, postData); - }; - - promiseToGetPost(request).then(function (postData) + return new Promise(function(resolve, reject) + { + utils.getPostData(request).then(function (postData) { - return promiseToDisplayContents(postData); + return require("../admin/addCategory.js").main(result, postData); }).then(function() { + console.log("admin page ended"); resolve(); }); }); diff --git a/img/image.js b/img/image.js index fddfdd4..12de61e 100644 --- a/img/image.js +++ b/img/image.js @@ -2,12 +2,15 @@ const utils = require('../utils/utils.js'); module.exports= { - main: function(res, fileName) + /**Sends the user an image from the specified fileName. + * + * @param result + * @param fileName + */ + main: function(result, fileName) { - path = "." + fileName; - - res.contentType = 'image/png'; - utils.include(res, path); - res.end(); + result.contentType = 'image/png'; + utils.include(result, "." + fileName); + result.end(); } }; \ No newline at end of file diff --git a/includes/includes.js b/includes/includes.js index 0c2aa8f..ac7c0fa 100644 --- a/includes/includes.js +++ b/includes/includes.js @@ -8,18 +8,38 @@ const HEADER_FILE = "includes/header.html"; const FOOTER_FILE = "includes/footer.html"; +const Promise = require('promise'); + module.exports = { - printHeader: function(res) + /** Appends the header html section to the result which is + * sent to the user. + * + * @param result + * @return {*} a promise retrieved from the utils.include function + */ + printHeader: function(result) { - res.writeHead(200, {'Content-Type': 'text/html'}); - utils.include(res, HEADER_FILE); - return 0; + result.writeHead(200, {'Content-Type': 'text/html'}); + return utils.include(result, HEADER_FILE); }, - printFooter: function(res) + /** + * Appends the footer to the result object + * + * @param result + * @return {*|Promise} + */ + printFooter: function(result) { - utils.include(res, FOOTER_FILE); - res.end(); - return 0; + return new Promise(function(resolve, reject) + { + console.log(FOOTER_FILE); + utils.include(result, FOOTER_FILE).then(function() + { + result.end(); + resolve(); + }) + + }) } }; \ No newline at end of file diff --git a/posts/category.js b/posts/category.js index 9c8154e..32f3a60 100644 --- a/posts/category.js +++ b/posts/category.js @@ -7,17 +7,7 @@ module.exports= { return new Promise(function(resolve, reject) { - sql.getCategories().then(function(categories) - { - console.log(categories); - for(var category in categories) - { - res.write(category); - } - }).then(function()) - { - resolve(); - } + resolve(); }); } }; \ No newline at end of file diff --git a/server.js b/server.js index 8bcf7bc..f0263f1 100644 --- a/server.js +++ b/server.js @@ -11,8 +11,6 @@ const url = require('url'); const includes = require('./includes/includes.js'); -var Promise = require('promise'); - http.createServer(function (request, res) { var q = url.parse(request.url, true); @@ -27,66 +25,25 @@ http.createServer(function (request, res) { var file = ""; - if(filename.includes("/category")) - { - //categories or view a category page + if(filename.includes("/category")) //single category page file = "../posts/category.js"; - } - else if(filename.includes("/admin")) - { - //admin page + + else if(filename.includes("/admin")) //top secret admin page file = "./admin/admin.js"; - } - else - { - //normal blog entry + + else //single post page file = "./posts/posts.js"; - } - var displayHeader = function() - { - return new Promise(function(resolve, reject) - { - var status = includes.printHeader(res); - if(status == 0) - { - console.log("Header done"); - resolve(); - } - }); - }; - var displayContent = function() + includes.printHeader(res).then(function() { - return new Promise(function(resolve, reject) - { - require(file).main(res, filename, request).then(function() - { - resolve(); - }); - }); - }; - var displayFooter = function() + return require(file).main(res, filename, request); + }).then(function() { - return new Promise(function(resolve, reject) - { - var status = includes.printFooter(res); - if(status == 0) - { - console.log("Footer done"); - resolve(); - } - }); - }; - - displayHeader().then(function() + return includes.printFooter(res); + }).then(function() { - return displayContent(); - }).then(function(){ - return displayFooter() - }).then(function(){ - console.log("finished!!!!!!!!!!!!!!!") + console.log("fin"); //for debugging }) - } }).listen(8080); \ No newline at end of file diff --git a/sidebar/categoriesSideBar.js b/sidebar/categoriesSideBar.js index b63c679..95b9c4f 100644 --- a/sidebar/categoriesSideBar.js +++ b/sidebar/categoriesSideBar.js @@ -14,49 +14,34 @@ module.exports= { return new Promise(function(resolve, reject) { - res.write("
"); + res.write("
"); + + res.write("
\n" + + "\n" + + "

Categories

\n" + + "\n" + + "
"); + + + res.write("
"); + + res.write("
    "); sql.getCategories().then(function(categories) { console.log(categories[0].name); - console.log("cool beans"); - // for(var category in categories) - // { - // console.log(category); - // res.write(category.name); - // } - res.write("
"); + categories.forEach(function(cat) + { + //res.write(cat.name); + console.log(cat); + + res.write("" + cat.name + ""); + res.write("
"); + }); + res.write(""); + res.write("
"); resolve(); }) + }); } - }; - -/* - - -
- -
- -

Tags

- -
- -
- -

Travel New York London - - IKEA NORWAY DIY - - Ideas Baby Family - - News Clothing Shopping - - Sports Games - -

- -
- -
- */ \ No newline at end of file + }; \ No newline at end of file diff --git a/utils/sql.js b/utils/sql.js index 7b3a314..11e9adb 100644 --- a/utils/sql.js +++ b/utils/sql.js @@ -27,7 +27,6 @@ var fetch = function(sqlStatement) console.log("sql fetch method called"); return new Promise(function(resolve, reject) { - con.query(sqlStatement, function (err, result) { if (err) diff --git a/utils/utils.js b/utils/utils.js index 496e5d9..d6ad60d 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -38,29 +38,38 @@ module.exports= * @param request sent by user in initial server call * @return the post data */ - getPostData: function(request) + getPostData: function(req) { - console.log("Get post data method"); - if(request.method == 'POST') + return new Promise(function(resolve, reject) { - var body = ''; - - request.on('data', function (data) + if(req.method == 'POST') { - body += data; + var body = ''; + + req.on('data', function (data) + { + body += data; + + //Kills request, don't steal my RAM!! + //You can only download so much ram ;) + if (body.length > 1e6) + { + req.connection.destroy(); + reject(); + } - //Kills request, don't steal my RAM!! - //You can only download so much ram ;) - if (body.length > 1e6) - request.connection.destroy(); - }); + }); - request.on('end', function () + req.on('end', function () + { + console.log(body); + resolve(body); + }); + } + else { - console.log(body); - return body; - }); - } - return {}; + resolve(0); + } + }); } }; \ No newline at end of file