Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

78 lines
2.1 KiB

var Promise = require('promise');
const sql = require('../utils/sql');
const utils = require('../utils/utils.js');
/**
* Renders all posts in a single category
*
* @param resultURL
* @returns {*}
*/
var renderPosts = function(resultURL)
{
var splitURL = resultURL.split("/");
if(splitURL.length >= 3)
{
return new Promise(function(resolve, reject)
{
sql.getPostsFromCategory(splitURL[2]).then(function(posts)
{
var promises = [];
posts.forEach(function(p)
{
promises.push(new Promise(function(res, rej)
{
require("../posts/singlePost.js")
.renderPreview(p).then(function(html)
{
res(html);
}).catch(function(error)
{
rej(error);
})
}));
});
Promise.all(promises).then(function(content)
{
resolve("<div class='col-md-8'>" + content.join('') + "</div>");
}).catch(function(error)
{
reject(error);
});
}).catch(function(err)
{
reject(err);
})
});
}
else
{
return utils.print404();
}
};
module.exports=
{
/**
* Calls posts and sidebar modules to render blog contents in order
*
* @param res
* @param fileName request url
*/
main: function(requestURL, request)
{
return new Promise(function(resolve, reject)
{
Promise.all([renderPosts(requestURL),
require("../sidebar/sidebar.js").main()]).then(function(content)
{
resolve(content.join(''));
}).catch(function(err)
{
reject(err);
})
});
}
};