diff --git a/includes/includes.js b/includes/includes.js
index 640685d..822abd0 100644
--- a/includes/includes.js
+++ b/includes/includes.js
@@ -20,7 +20,6 @@ module.exports =
*/
printHeader: function()
{
- // utils.getFileContents(HEADER_FILE);
return utils.include(HEADER_FILE);
},
@@ -33,10 +32,6 @@ module.exports =
printFooter: function()
{
return utils.include(FOOTER_FILE);
- // return new Promise(function(resolve, reject)
- // {
- // resolve(utils.getFileContents(FOOTER_FILE));
- // })
},
diff --git a/posts/category.js b/posts/category.js
index 1c1ccf5..1e4e72e 100644
--- a/posts/category.js
+++ b/posts/category.js
@@ -6,17 +6,14 @@ const utils = require('../utils/utils.js');
/**
* Renders all posts in a single category
*
- * @param result
* @param resultURL
* @returns {*}
*/
-var renderPosts = function(result, resultURL)
+var renderPosts = function(resultURL)
{
var splitURL = resultURL.split("/");
if(splitURL.length >= 3)
{
- result.write("
");
-
return new Promise(function(resolve, reject)
{
sql.getPostsFromCategory(splitURL[2]).then(function(posts)
@@ -27,26 +24,32 @@ var renderPosts = function(result, resultURL)
promises.push(new Promise(function(res, rej)
{
require("../posts/singlePost.js")
- .renderPreview(result, p).then(function()
+ .renderPreview(p).then(function(html)
+ {
+ res(html);
+ }).catch(function(error)
{
- res();
- });
+ rej(error);
+ })
}));
});
- return Promise.all(promises);
- }).then(function()
- {
- result.write("
");
- resolve();
+
+ Promise.all(promises).then(function(content)
+ {
+ resolve("" + content.join('') + "
");
+ }).catch(function(error)
+ {
+ reject(error);
+ });
}).catch(function(err)
{
- console.log(err);
+ reject(err);
})
});
}
else
{
- return utils.print404(result);
+ return utils.print404();
}
};
@@ -58,21 +61,18 @@ module.exports=
* @param res
* @param fileName request url
*/
- main: function(res, requestURL, request)
+ main: function(requestURL, request)
{
return new Promise(function(resolve, reject)
{
- renderPosts(res, requestURL).then(function()
+ Promise.all([renderPosts(requestURL),
+ require("../sidebar/sidebar.js").main()]).then(function(content)
{
- return require("../sidebar/sidebar.js").main(res)
- }).then(function ()
+ resolve(content.join(''));
+ }).catch(function(err)
{
- resolve();
- }).catch(function(error)
- {
- console.log(error);
- reject(error);
+ reject(err);
})
});
}
- }
\ No newline at end of file
+ };
\ No newline at end of file
diff --git a/posts/homePage.js b/posts/homePage.js
index 948fc28..7688c9b 100644
--- a/posts/homePage.js
+++ b/posts/homePage.js
@@ -15,7 +15,6 @@ var renderRecentPosts = function()
{
return new Promise(function(resolve, reject)
{
- console.log("recent post render page");
sql.getRecentPostSQL().then(function(posts)
{
var postPromises = [];
@@ -61,23 +60,8 @@ module.exports=
{
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);
- // })
-
-
- console.log("home page");
Promise.all([renderRecentPosts(), require("../sidebar/sidebar.js").main()]).then(function(content)
{
- console.log("fin posts");
resolve(content.join(''));
}).catch(function(error)
{
diff --git a/posts/posts.js b/posts/posts.js
index e1d2dad..fda63f0 100644
--- a/posts/posts.js
+++ b/posts/posts.js
@@ -1,6 +1,6 @@
const utils = require('../utils/utils.js');
const sql = require('../utils/sql');
-var Promise = require('promise');
+const Promise = require('promise');
/**
@@ -11,11 +11,10 @@ var Promise = require('promise');
* @param requestURL url requested from client
* @return {*|Promise} returns a resolved promise to preserve execution order
*/
-var renderPost = function(res, requestURL)
+const renderPost = function(requestURL)
{
return new Promise(function(resolve, reject)
{
- res.write("");
var splitURL = requestURL.split("/");
//user entered /category/name/ or /category/name
@@ -25,24 +24,25 @@ var renderPost = function(res, requestURL)
{
if(post != 0)
{
- return require("../posts/singlePost.js").renderPost(res, post);
+ return require("../posts/singlePost.js").renderPost(post);
}
else
{
- return utils.print404(res);
+ return utils.print404();
}
- }).then(function()
+ }).then(function(html)
{
- res.write("
");
- resolve();
+ resolve("" + html + "
");
+ }).catch(function(error)
+ {
+ reject(error);
})
}
else
{
- utils.print404(res).then(function()
+ utils.print404().then(function(html)
{
- res.write("");
- resolve();
+ resolve("" + html + "
");
});
}
});
@@ -53,20 +53,19 @@ module.exports=
/**
* Calls posts and sidebar modules to render blog contents in order
*
- * @param res
* @param fileName request url
*/
- main: function(res, requestURL, request)
+ main: function(requestURL, request)
{
return new Promise(function(resolve, reject)
{
- renderPost(res, requestURL).then(function()
+ Promise.all([renderPost(requestURL), require("../sidebar/sidebar.js").main()]).then(function(content)
{
- return require("../sidebar/sidebar.js").main(res);
- }).then(function ()
+ resolve(content.join(''));
+ }).catch(function(error)
{
- resolve();
- });
+ reject(error);
+ })
});
}
};
\ No newline at end of file
diff --git a/posts/singlePost.js b/posts/singlePost.js
index 2c4aa14..bbf4b3d 100644
--- a/posts/singlePost.js
+++ b/posts/singlePost.js
@@ -92,7 +92,6 @@ module.exports=
resolve(html)
}).catch(function(error)
{
- console.log(error);
reject(error);
});
}
@@ -145,7 +144,6 @@ module.exports=
html = html.split("
";
-
resolve(html);
});
}
diff --git a/server.js b/server.js
index 7efaa13..ed84e53 100644
--- a/server.js
+++ b/server.js
@@ -32,14 +32,10 @@ const port = 8000;
*/
app.use(function(request, res)
{
-
- console.log(request.headers.host);
-
if(request.headers.host.includes("localhost:" + port) ||
request.headers.host.includes("jrtechs.net"))
{
- var q = url.parse(request.url, true);
- var filename = q.pathname;
+ const filename = url.parse(request.url, true).pathname;
//handles image requests
if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png"))
@@ -76,31 +72,18 @@ app.use(function(request, res)
file = "./posts/posts.js";
}
- // 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)
{
- console.log("main fin");
res.write(content.join(''));
res.end();
}).catch(function(err)
{
console.log(err);
});
-
}
}
else
diff --git a/sidebar/popularPosts.js b/sidebar/popularPosts.js
index f27d874..da4b929 100644
--- a/sidebar/popularPosts.js
+++ b/sidebar/popularPosts.js
@@ -10,7 +10,6 @@ module.exports=
*/
main: function(res)
{
- console.log("sidebar called");
return new Promise(function(resolve, reject)
{
res.write("");
diff --git a/sidebar/recentPosts.js b/sidebar/recentPosts.js
index 308baad..7c8f8d3 100644
--- a/sidebar/recentPosts.js
+++ b/sidebar/recentPosts.js
@@ -11,13 +11,8 @@ module.exports=
{
return new Promise(function(resolve, reject)
{
-
- console.log("recent page");
-
-
var content = "
";
-
content +="
";
content +="
\n" +
diff --git a/sidebar/sidebar.js b/sidebar/sidebar.js
index 378e11b..c301a2f 100644
--- a/sidebar/sidebar.js
+++ b/sidebar/sidebar.js
@@ -13,27 +13,6 @@ module.exports=
{
return new Promise(function(resolve, reject)
{
- console.log("sidebar page");
-
- // res.write("");
- //
- // 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("
");
- // 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)
diff --git a/utils/sql.js b/utils/sql.js
index f78da4e..cdb5495 100644
--- a/utils/sql.js
+++ b/utils/sql.js
@@ -131,7 +131,6 @@ module.exports=
{
resolve(0);
}
-
});
});
diff --git a/utils/utils.js b/utils/utils.js
index de4e2f1..291fa2b 100644
--- a/utils/utils.js
+++ b/utils/utils.js
@@ -22,7 +22,6 @@ module.exports=
try
{
resolve(fs.readFileSync(fileName));
-
}
catch (e)
{
@@ -117,9 +116,9 @@ module.exports=
* @param result
* @returns {*}
*/
- print404: function(result)
+ print404: function()
{
- return this.include(result, "includes/404.html");
+ return this.include("includes/404.html");
},
@@ -129,8 +128,8 @@ module.exports=
* @param result
* @returns {*}
*/
- printWrongHost: function(result)
+ printWrongHost: function()
{
- return this.include(result, "includes/incorrectHost.html");
+ return this.include("includes/incorrectHost.html");
}
};
\ No newline at end of file