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.

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