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.

134 lines
3.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. const http = require('http');
  8. const url = require('url');
  9. const express = require("express");
  10. //const session = require('express-session');
  11. const includes = require('./includes/includes.js');
  12. const sql = require('./utils/sql');
  13. const map = require('./utils/generateSiteMap.js');
  14. map.main();
  15. const app = express();
  16. //app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }}));
  17. //compresses the site
  18. var compression = require('compression')
  19. app.use(compression());
  20. const port = 8000;
  21. const cache = require('memory-cache');
  22. /**
  23. * Parses the request url and calls correct JS files
  24. */
  25. app.use(function(request, res)
  26. {
  27. if(request.headers.host.includes("localhost:" + port) ||
  28. request.headers.host.includes("jrtechs.net"))
  29. {
  30. const filename = url.parse(request.url, true).pathname;
  31. //handles image requests
  32. if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png") || filename.includes(".ico"))
  33. {
  34. require("./img/image.js").main(res, filename, cache);
  35. }
  36. else if(filename.includes("/css/") || filename.includes(".woff2"))
  37. {
  38. includes.sendCSS(res, filename, cache)
  39. }
  40. else if(filename.includes("/js/") || filename.includes(".js"))
  41. {
  42. require("./js/js.js").main(res, filename, cache);
  43. }
  44. else if(filename.includes("/downloads/"))
  45. {
  46. require("./downloads/downloads.js").main(res, filename, request);
  47. }
  48. else
  49. {
  50. var file = "";
  51. var html = cache.get(filename);
  52. res.writeHead(200, {'Content-Type': 'text/html'});
  53. if(html == null)
  54. {
  55. if(filename === '' || filename === '/')
  56. {
  57. file="./posts/homePage.js";
  58. }
  59. else
  60. {
  61. var urlSplit = filename.split("/");
  62. if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
  63. file = "./posts/category.js";
  64. else
  65. file = "./posts/posts.js";
  66. }
  67. Promise.all([includes.printHeader(),
  68. require(file).main(filename, request),
  69. includes.printFooter()]).then(function(content)
  70. {
  71. res.write(content.join(''));
  72. res.end();
  73. cache.put(filename, content.join(''));
  74. }).catch(function(err)
  75. {
  76. console.log(err);
  77. throw err;
  78. });
  79. }
  80. else
  81. {
  82. res.write(html);
  83. res.end();
  84. }
  85. try
  86. {
  87. var getClientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
  88. || request.connection.remoteAddress;
  89. console.log(getClientAddress);
  90. sql.logTraffic(getClientAddress, filename);
  91. }
  92. catch (e)
  93. {
  94. }
  95. }
  96. }
  97. else
  98. {
  99. // utils.printWrongHost(res);
  100. res.writeHead(418, {});
  101. res.end();
  102. }
  103. });
  104. http.createServer(app).listen(port);