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.

167 lines
4.4 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. //http server
  8. const http = require('http');
  9. //used to parse the request URL
  10. const url = require('url');
  11. //express app
  12. const express = require("express");
  13. //express app
  14. const app = express();
  15. //used to append static content to result
  16. const includes = require('./includes/includes.js');
  17. //server side logging
  18. const sql = require('./utils/sql');
  19. //Used for gzip compression
  20. const compression = require('compression')
  21. //caching program to make the application run faster
  22. const cache = require('memory-cache');
  23. //Updates the site map whenever the server is started
  24. const map = require('./utils/generateSiteMap.js');
  25. map.main();
  26. //port for the server to run on
  27. const port = 8000;
  28. const projects = ["/steam/"];
  29. /**
  30. * Parses the request url and calls correct JS files
  31. */
  32. app.use(function(request, result)
  33. {
  34. //prevents people from pointing their dns at my IP:port for my site
  35. if(request.headers.host.includes("localhost:" + port) ||
  36. request.headers.host.includes("jrtechs.net"))
  37. {
  38. const filename = url.parse(request.url, true).pathname;
  39. var project = false;
  40. projects.forEach(function(projectName)
  41. {
  42. if(filename.includes(projectName))
  43. {
  44. require("./includes/projects.js").main(request, result, projectName);
  45. project = true;
  46. }
  47. });
  48. if(project)
  49. {
  50. //don't do blog stuff
  51. }
  52. //handles image requests
  53. else if(filename.includes("/img/") || filename.includes(".jpg") ||
  54. filename.includes(".png") || filename.includes(".ico")
  55. || filename.includes(".svg"))
  56. {
  57. includes.sendImage(result, filename, cache);
  58. }
  59. //css and font files
  60. else if(filename.includes("/css/") || filename.includes(".woff2") ||
  61. filename.includes(".txt"))
  62. {
  63. includes.sendCSS(result, filename, cache);
  64. }
  65. //scripts
  66. else if(filename.includes("/js/") || filename.includes(".js"))
  67. {
  68. includes.sendJS(result, filename, cache);
  69. }
  70. //downloads
  71. else if(filename.includes("/downloads/"))
  72. {
  73. require("./includes/downloads.js").main(result, filename);
  74. }
  75. else if(filename.includes("/contact"))
  76. {
  77. require("./includes/contact.js").main(request, result);
  78. }
  79. else
  80. {
  81. const html = cache.get(filename);
  82. result.writeHead(200, {'Content-Type': 'text/html'});
  83. if(html == null)
  84. {
  85. var file = "";
  86. if(filename === '' || filename === '/')
  87. {
  88. file="./posts/homePage.js";
  89. }
  90. else
  91. {
  92. var urlSplit = filename.split("/");
  93. if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
  94. file = "./posts/category.js";
  95. else
  96. file = "./posts/posts.js";
  97. }
  98. Promise.all([includes.printHeader(),
  99. require(file).main(filename, request),
  100. includes.printFooter()]).then(function(content)
  101. {
  102. result.write(content.join(''));
  103. result.end();
  104. cache.put(filename, content.join(''));
  105. }).catch(function(err)
  106. {
  107. console.log(err);
  108. throw err;
  109. });
  110. }
  111. else
  112. {
  113. result.write(html);
  114. result.end();
  115. }
  116. try
  117. {
  118. const getClientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
  119. || request.connection.remoteAddress;
  120. console.log(getClientAddress);
  121. sql.logTraffic(getClientAddress, filename);
  122. }
  123. catch (e)
  124. { }
  125. }
  126. }
  127. else
  128. {
  129. // utils.printWrongHost(result);
  130. result.writeHead(418, {});
  131. result.end();
  132. }
  133. });
  134. //enables gzip compression for the site
  135. app.use(compression());
  136. http.createServer(app).listen(port);