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.

147 lines
3.9 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, request);
  59. }
  60. else
  61. {
  62. const html = cache.get(filename);
  63. result.writeHead(200, {'Content-Type': 'text/html'});
  64. if(html == null)
  65. {
  66. var file = "";
  67. if(filename === '' || filename === '/')
  68. {
  69. file="./posts/homePage.js";
  70. }
  71. else
  72. {
  73. var urlSplit = filename.split("/");
  74. if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
  75. file = "./posts/category.js";
  76. else
  77. file = "./posts/posts.js";
  78. }
  79. Promise.all([includes.printHeader(),
  80. require(file).main(filename, request),
  81. includes.printFooter()]).then(function(content)
  82. {
  83. result.write(content.join(''));
  84. result.end();
  85. cache.put(filename, content.join(''));
  86. }).catch(function(err)
  87. {
  88. console.log(err);
  89. throw err;
  90. });
  91. }
  92. else
  93. {
  94. result.write(html);
  95. result.end();
  96. }
  97. try
  98. {
  99. var getClientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
  100. || request.connection.remoteAddress;
  101. console.log(getClientAddress);
  102. sql.logTraffic(getClientAddress, filename);
  103. }
  104. catch (e)
  105. {
  106. }
  107. }
  108. }
  109. else
  110. {
  111. // utils.printWrongHost(result);
  112. result.writeHead(418, {});
  113. result.end();
  114. }
  115. });
  116. //enables gzip compression for the site
  117. app.use(compression());
  118. http.createServer(app).listen(port);