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.

170 lines
4.5 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.startsWith(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. else if(filename.includes(".svg") || filename.includes(".svg"))
  53. {
  54. result.writeHead(200, {'Content-Type': 'image/svg+xml'});
  55. }
  56. //handles image requests
  57. else if(filename.includes("/img/") || filename.includes(".jpg") ||
  58. filename.includes(".png") || filename.includes(".ico"))
  59. {
  60. includes.sendImage(result, filename, cache);
  61. }
  62. //css and font files
  63. else if(filename.includes("/css/") || filename.includes(".woff2") ||
  64. filename.includes(".txt"))
  65. {
  66. includes.sendCSS(result, filename, cache);
  67. }
  68. //scripts
  69. else if(filename.includes("/js/") || filename.includes(".js"))
  70. {
  71. includes.sendJS(result, filename, cache);
  72. }
  73. //downloads
  74. else if(filename.includes("/downloads/"))
  75. {
  76. require("./includes/downloads.js").main(result, filename);
  77. }
  78. else if(filename.includes("/contact"))
  79. {
  80. require("./includes/contact.js").main(request, result);
  81. }
  82. else
  83. {
  84. const html = cache.get(filename);
  85. result.writeHead(200, {'Content-Type': 'text/html'});
  86. if(html == null)
  87. {
  88. var file = "";
  89. if(filename === '' || filename === '/')
  90. {
  91. file="./posts/homePage.js";
  92. }
  93. else
  94. {
  95. var urlSplit = filename.split("/");
  96. if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
  97. file = "./posts/category.js";
  98. else
  99. file = "./posts/posts.js";
  100. }
  101. Promise.all([includes.printHeader(),
  102. require(file).main(filename, request),
  103. includes.printFooter()]).then(function(content)
  104. {
  105. result.write(content.join(''));
  106. result.end();
  107. cache.put(filename, content.join(''));
  108. }).catch(function(err)
  109. {
  110. console.log(err);
  111. throw err;
  112. });
  113. }
  114. else
  115. {
  116. result.write(html);
  117. result.end();
  118. }
  119. try
  120. {
  121. const getClientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
  122. || request.connection.remoteAddress;
  123. console.log(getClientAddress);
  124. sql.logTraffic(getClientAddress, filename);
  125. }
  126. catch (e)
  127. { }
  128. }
  129. }
  130. else
  131. {
  132. // utils.printWrongHost(result);
  133. result.writeHead(418, {});
  134. result.end();
  135. }
  136. });
  137. //enables gzip compression for the site
  138. app.use(compression());
  139. http.createServer(app).listen(port);