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.

92 lines
2.2 KiB

  1. /**
  2. * Main server file for the blog. This file is responsible for
  3. * creating the server and listening for clients. The main run
  4. * function parses the url and calls a sub module to make the
  5. * appropriate pages.
  6. */
  7. const http = require('http');
  8. const url = require('url');
  9. const includes = require('./includes/includes.js');
  10. var Promise = require('promise');
  11. http.createServer(function (request, res)
  12. {
  13. var q = url.parse(request.url, true);
  14. var filename = q.pathname;
  15. //handles image requests
  16. if(filename.includes("/img/"))
  17. {
  18. require("./img/image.js").main(res, filename);
  19. }
  20. else
  21. {
  22. var file = "";
  23. if(filename.includes("/category"))
  24. {
  25. //categories or view a category page
  26. file = "../posts/category.js";
  27. }
  28. else if(filename.includes("/admin"))
  29. {
  30. //admin page
  31. file = "./admin/admin.js";
  32. }
  33. else
  34. {
  35. //normal blog entry
  36. file = "./posts/posts.js";
  37. }
  38. var displayHeader = function()
  39. {
  40. return new Promise(function(resolve, reject)
  41. {
  42. var status = includes.printHeader(res);
  43. if(status == 0)
  44. {
  45. console.log("Header done");
  46. resolve();
  47. }
  48. });
  49. };
  50. var displayContent = function()
  51. {
  52. return new Promise(function(resolve, reject)
  53. {
  54. require(file).main(res, filename, request).then(function(){
  55. resolve();
  56. });
  57. });
  58. };
  59. var displayFooter = function()
  60. {
  61. return new Promise(function(resolve, reject)
  62. {
  63. var status = includes.printFooter(res);
  64. if(status == 0)
  65. {
  66. console.log("Footer done");
  67. resolve();
  68. }
  69. });
  70. };
  71. displayHeader().then(function()
  72. {
  73. return displayContent();
  74. }).then(function(){
  75. return displayFooter()
  76. }).then(function(){
  77. console.log("finished!!!!!!!!!!!!!!!")
  78. })
  79. }
  80. }).listen(8080);