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.

137 lines
3.6 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 express = require("express");
  10. //const session = require('express-session');
  11. const includes = require('./includes/includes.js');
  12. const map = require('./utils/generateSiteMap.js');
  13. map.main();
  14. const app = express();
  15. //app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }}));
  16. //compresses the site
  17. var compression = require('compression')
  18. app.use(compression());
  19. const port = 8000;
  20. const cache = require('memory-cache');
  21. /**
  22. * Parses the request url and calls correct JS files
  23. */
  24. app.use(function(request, res)
  25. {
  26. if(request.headers.host.includes("localhost:" + port) ||
  27. request.headers.host.includes("jrtechs.net"))
  28. {
  29. const filename = url.parse(request.url, true).pathname;
  30. //handles image requests
  31. if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png"))
  32. {
  33. require("./img/image.js").main(res, filename, cache);
  34. }
  35. else if(filename.includes("/css/") || filename.includes(".woff2"))
  36. {
  37. includes.sendCSS(res, filename, cache)
  38. }
  39. else if(filename.includes("/js/") || filename.includes(".js"))
  40. {
  41. require("./js/js.js").main(res, filename, cache);
  42. }
  43. else if(filename.includes("/downloads/"))
  44. {
  45. require("./downloads/downloads.js").main(res, filename, request);
  46. }
  47. else if(filename.includes("/admin"))
  48. {
  49. res.writeHead(200, {'Content-Type': 'text/html'});
  50. file = "./admin/admin.js";
  51. Promise.all([includes.printHeader(),
  52. require(file).main(filename, request),
  53. includes.printFooter()]).then(function(content)
  54. {
  55. res.write(content.join(''));
  56. res.end();
  57. }).catch(function(err)
  58. {
  59. console.log(err);
  60. throw err;
  61. });
  62. }
  63. else
  64. {
  65. var file = "";
  66. var html = cache.get(filename);
  67. res.writeHead(200, {'Content-Type': 'text/html'});
  68. if(html == null)
  69. {
  70. if(filename === '' || filename === '/')
  71. {
  72. file="./posts/homePage.js";
  73. }
  74. else
  75. {
  76. var urlSplit = filename.split("/");
  77. if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
  78. file = "./posts/category.js";
  79. else
  80. file = "./posts/posts.js";
  81. }
  82. Promise.all([includes.printHeader(),
  83. require(file).main(filename, request),
  84. includes.printFooter()]).then(function(content)
  85. {
  86. res.write(content.join(''));
  87. res.end();
  88. cache.put(filename, content.join(''));
  89. }).catch(function(err)
  90. {
  91. console.log(err);
  92. throw err;
  93. });
  94. }
  95. else
  96. {
  97. res.write(html);
  98. res.end();
  99. }
  100. }
  101. }
  102. else
  103. {
  104. // utils.printWrongHost(res);
  105. res.writeHead(418, {});
  106. res.end();
  107. }
  108. });
  109. http.createServer(app).listen(port);