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.
 
 

93 lines
2.2 KiB

/**
* Main server file for the blog. This file is responsible for
* creating the server and listening for clients. The main run
* function parses the url and calls a sub module to make the
* appropriate pages.
*/
const http = require('http');
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);
var filename = q.pathname;
//handles image requests
if(filename.includes("/img/"))
{
require("./img/image.js").main(res, filename);
}
else
{
var file = "";
if(filename.includes("/category"))
{
//categories or view a category page
file = "../posts/category.js";
}
else if(filename.includes("/admin"))
{
//admin page
file = "./admin/admin.js";
}
else
{
//normal blog entry
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()
{
return new Promise(function(resolve, reject)
{
require(file).main(res, filename, request).then(function(){
resolve();
});
});
};
var displayFooter = function()
{
return new Promise(function(resolve, reject)
{
var status = includes.printFooter(res);
if(status == 0)
{
console.log("Footer done");
resolve();
}
});
};
displayHeader().then(function()
{
return displayContent();
}).then(function(){
return displayFooter()
}).then(function(){
console.log("finished!!!!!!!!!!!!!!!")
})
}
}).listen(8080);