Browse Source

Updated category and sidebars pages to do stuff in a "perfect" async fashion.

pull/4/head
jrtechs 6 years ago
parent
commit
41de3d6e5d
11 changed files with 46 additions and 116 deletions
  1. +0
    -5
      includes/includes.js
  2. +24
    -24
      posts/category.js
  3. +0
    -16
      posts/homePage.js
  4. +17
    -18
      posts/posts.js
  5. +0
    -2
      posts/singlePost.js
  6. +1
    -18
      server.js
  7. +0
    -1
      sidebar/popularPosts.js
  8. +0
    -5
      sidebar/recentPosts.js
  9. +0
    -21
      sidebar/sidebar.js
  10. +0
    -1
      utils/sql.js
  11. +4
    -5
      utils/utils.js

+ 0
- 5
includes/includes.js View File

@ -20,7 +20,6 @@ module.exports =
*/ */
printHeader: function() printHeader: function()
{ {
// utils.getFileContents(HEADER_FILE);
return utils.include(HEADER_FILE); return utils.include(HEADER_FILE);
}, },
@ -33,10 +32,6 @@ module.exports =
printFooter: function() printFooter: function()
{ {
return utils.include(FOOTER_FILE); return utils.include(FOOTER_FILE);
// return new Promise(function(resolve, reject)
// {
// resolve(utils.getFileContents(FOOTER_FILE));
// })
}, },

+ 24
- 24
posts/category.js View File

@ -6,17 +6,14 @@ const utils = require('../utils/utils.js');
/** /**
* Renders all posts in a single category * Renders all posts in a single category
* *
* @param result
* @param resultURL * @param resultURL
* @returns {*} * @returns {*}
*/ */
var renderPosts = function(result, resultURL)
var renderPosts = function(resultURL)
{ {
var splitURL = resultURL.split("/"); var splitURL = resultURL.split("/");
if(splitURL.length >= 3) if(splitURL.length >= 3)
{ {
result.write("<div class='col-md-8'>");
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
sql.getPostsFromCategory(splitURL[2]).then(function(posts) sql.getPostsFromCategory(splitURL[2]).then(function(posts)
@ -27,26 +24,32 @@ var renderPosts = function(result, resultURL)
promises.push(new Promise(function(res, rej) promises.push(new Promise(function(res, rej)
{ {
require("../posts/singlePost.js") 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("</div>");
resolve();
Promise.all(promises).then(function(content)
{
resolve("<div class='col-md-8'>" + content.join('') + "</div>");
}).catch(function(error)
{
reject(error);
});
}).catch(function(err) }).catch(function(err)
{ {
console.log(err);
reject(err);
}) })
}); });
} }
else else
{ {
return utils.print404(result);
return utils.print404();
} }
}; };
@ -58,21 +61,18 @@ module.exports=
* @param res * @param res
* @param fileName request url * @param fileName request url
*/ */
main: function(res, requestURL, request)
main: function(requestURL, request)
{ {
return new Promise(function(resolve, reject) 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);
}) })
}); });
} }
}
};

+ 0
- 16
posts/homePage.js View File

@ -15,7 +15,6 @@ 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 = [];
@ -61,23 +60,8 @@ module.exports=
{ {
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);
// })
console.log("home page");
Promise.all([renderRecentPosts(), require("../sidebar/sidebar.js").main()]).then(function(content) Promise.all([renderRecentPosts(), require("../sidebar/sidebar.js").main()]).then(function(content)
{ {
console.log("fin posts");
resolve(content.join('')); resolve(content.join(''));
}).catch(function(error) }).catch(function(error)
{ {

+ 17
- 18
posts/posts.js View File

@ -1,6 +1,6 @@
const utils = require('../utils/utils.js'); const utils = require('../utils/utils.js');
const sql = require('../utils/sql'); 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 * @param requestURL url requested from client
* @return {*|Promise} returns a resolved promise to preserve execution order * @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) return new Promise(function(resolve, reject)
{ {
res.write("<div class='col-md-8'>");
var splitURL = requestURL.split("/"); var splitURL = requestURL.split("/");
//user entered /category/name/ or /category/name //user entered /category/name/ or /category/name
@ -25,24 +24,25 @@ var renderPost = function(res, requestURL)
{ {
if(post != 0) if(post != 0)
{ {
return require("../posts/singlePost.js").renderPost(res, post);
return require("../posts/singlePost.js").renderPost(post);
} }
else else
{ {
return utils.print404(res);
return utils.print404();
} }
}).then(function()
}).then(function(html)
{ {
res.write("</div>");
resolve();
resolve("<div class='col-md-8'>" + html + "</div>");
}).catch(function(error)
{
reject(error);
}) })
} }
else else
{ {
utils.print404(res).then(function()
utils.print404().then(function(html)
{ {
res.write("</div>");
resolve();
resolve("<div class='col-md-8'>" + html + "</div>");
}); });
} }
}); });
@ -53,20 +53,19 @@ module.exports=
/** /**
* Calls posts and sidebar modules to render blog contents in order * Calls posts and sidebar modules to render blog contents in order
* *
* @param res
* @param fileName request url * @param fileName request url
*/ */
main: function(res, requestURL, request)
main: function(requestURL, request)
{ {
return new Promise(function(resolve, reject) 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);
})
}); });
} }
}; };

+ 0
- 2
posts/singlePost.js View File

@ -92,7 +92,6 @@ module.exports=
resolve(html) resolve(html)
}).catch(function(error) }).catch(function(error)
{ {
console.log(error);
reject(error); reject(error);
}); });
} }
@ -145,7 +144,6 @@ module.exports=
html = html.split("<img").join("<img style=\"max-width: 100%;\" "); html = html.split("<img").join("<img style=\"max-width: 100%;\" ");
html += "</div></div></div><br><br>"; html += "</div></div></div><br><br>";
resolve(html); resolve(html);
}); });
} }

+ 1
- 18
server.js View File

@ -32,14 +32,10 @@ const port = 8000;
*/ */
app.use(function(request, res) app.use(function(request, res)
{ {
console.log(request.headers.host);
if(request.headers.host.includes("localhost:" + port) || if(request.headers.host.includes("localhost:" + port) ||
request.headers.host.includes("jrtechs.net")) 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 //handles image requests
if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png")) if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png"))
@ -76,31 +72,18 @@ app.use(function(request, res)
file = "./posts/posts.js"; 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'}); res.writeHead(200, {'Content-Type': 'text/html'});
Promise.all([includes.printHeader(), Promise.all([includes.printHeader(),
require(file).main(filename, request), require(file).main(filename, request),
includes.printFooter()]).then(function(content) includes.printFooter()]).then(function(content)
{ {
console.log("main fin");
res.write(content.join('')); res.write(content.join(''));
res.end(); res.end();
}).catch(function(err) }).catch(function(err)
{ {
console.log(err); console.log(err);
}); });
} }
} }
else else

+ 0
- 1
sidebar/popularPosts.js View File

@ -10,7 +10,6 @@ module.exports=
*/ */
main: function(res) main: function(res)
{ {
console.log("sidebar called");
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
res.write("<div class=\"w3-card w3-margin\">"); res.write("<div class=\"w3-card w3-margin\">");

+ 0
- 5
sidebar/recentPosts.js View File

@ -11,13 +11,8 @@ module.exports=
{ {
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
console.log("recent page");
var content = "<div class=\"container\">"; var content = "<div class=\"container\">";
content +="<div class=\"list-group\">"; content +="<div class=\"list-group\">";
content +=" <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" +

+ 0
- 21
sidebar/sidebar.js View File

@ -13,27 +13,6 @@ module.exports=
{ {
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
console.log("sidebar page");
// 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"), Promise.all([utils.include("sidebar/sidebar.html"),
require("../sidebar/recentPosts.js").main(), require("../sidebar/recentPosts.js").main(),
require("../sidebar/categoriesSideBar.js").main()]).then(function(content) require("../sidebar/categoriesSideBar.js").main()]).then(function(content)

+ 0
- 1
utils/sql.js View File

@ -131,7 +131,6 @@ module.exports=
{ {
resolve(0); resolve(0);
} }
}); });
}); });

+ 4
- 5
utils/utils.js View File

@ -22,7 +22,6 @@ module.exports=
try try
{ {
resolve(fs.readFileSync(fileName)); resolve(fs.readFileSync(fileName));
} }
catch (e) catch (e)
{ {
@ -117,9 +116,9 @@ module.exports=
* @param result * @param result
* @returns {*} * @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 * @param result
* @returns {*} * @returns {*}
*/ */
printWrongHost: function(result)
printWrongHost: function()
{ {
return this.include(result, "includes/incorrectHost.html");
return this.include("includes/incorrectHost.html");
} }
}; };

Loading…
Cancel
Save