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.

155 lines
4.1 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. /**
  29. * Parses the request url and calls correct JS files
  30. */
  31. app.use(function(request, result)
  32. {
  33. //prevents people from pointing their dns at my IP:port for my site
  34. if(request.headers.host.includes("localhost:" + port) ||
  35. request.headers.host.includes("jrtechs.net"))
  36. {
  37. const filename = url.parse(request.url, true).pathname;
  38. if (filename.includes("/steam/"))
  39. {
  40. require("./includes/projects.js").main(request, result);
  41. }
  42. //handles image requests
  43. else if(filename.includes("/img/") || filename.includes(".jpg") ||
  44. filename.includes(".png") || filename.includes(".ico"))
  45. {
  46. includes.sendImage(result, filename, cache);
  47. }
  48. //css and font files
  49. else if(filename.includes("/css/") || filename.includes(".woff2") ||
  50. filename.includes(".txt"))
  51. {
  52. includes.sendCSS(result, filename, cache);
  53. }
  54. //scripts
  55. else if(filename.includes("/js/") || filename.includes(".js"))
  56. {
  57. includes.sendJS(result, filename, cache);
  58. }
  59. //downloads
  60. else if(filename.includes("/downloads/"))
  61. {
  62. require("./includes/downloads.js").main(result, filename);
  63. }
  64. else if(filename.includes("/contact"))
  65. {
  66. require("./includes/contact.js").main(request, result);
  67. }
  68. else
  69. {
  70. const html = cache.get(filename);
  71. result.writeHead(200, {'Content-Type': 'text/html'});
  72. if(html == null)
  73. {
  74. var file = "";
  75. if(filename === '' || filename === '/')
  76. {
  77. file="./posts/homePage.js";
  78. }
  79. else
  80. {
  81. var urlSplit = filename.split("/");
  82. if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
  83. file = "./posts/category.js";
  84. else
  85. file = "./posts/posts.js";
  86. }
  87. Promise.all([includes.printHeader(),
  88. require(file).main(filename, request),
  89. includes.printFooter()]).then(function(content)
  90. {
  91. result.write(content.join(''));
  92. result.end();
  93. cache.put(filename, content.join(''));
  94. }).catch(function(err)
  95. {
  96. console.log(err);
  97. throw err;
  98. });
  99. }
  100. else
  101. {
  102. result.write(html);
  103. result.end();
  104. }
  105. try
  106. {
  107. const getClientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
  108. || request.connection.remoteAddress;
  109. console.log(getClientAddress);
  110. sql.logTraffic(getClientAddress, filename);
  111. }
  112. catch (e)
  113. { }
  114. }
  115. }
  116. else
  117. {
  118. // utils.printWrongHost(result);
  119. result.writeHead(418, {});
  120. result.end();
  121. }
  122. });
  123. //enables gzip compression for the site
  124. app.use(compression());
  125. http.createServer(app).listen(port);