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.

91 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. {
  56. resolve();
  57. });
  58. });
  59. };
  60. var displayFooter = function()
  61. {
  62. return new Promise(function(resolve, reject)
  63. {
  64. var status = includes.printFooter(res);
  65. if(status == 0)
  66. {
  67. console.log("Footer done");
  68. resolve();
  69. }
  70. });
  71. };
  72. displayHeader().then(function()
  73. {
  74. return displayContent();
  75. }).then(function(){
  76. return displayFooter()
  77. }).then(function(){
  78. console.log("finished!!!!!!!!!!!!!!!")
  79. })
  80. }
  81. }).listen(8080);