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.

104 lines
2.4 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. //server side logging
  16. const sql = require('./utils/sql');
  17. //Used for gzip compression
  18. const compression = require('compression');
  19. //used for file io
  20. const utils = require('./utils/utils.js');
  21. //Updates the site map whenever the server is started
  22. const map = require('./utils/generateSiteMap.js');
  23. map.main();
  24. //port for the server to run on
  25. const port = 8000;
  26. //session data for login
  27. const session = require('express-session');
  28. //Initializes sessions for login
  29. app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }}));
  30. const projects = ["/steam/"];
  31. /**
  32. * Parses the request url and calls correct JS files
  33. */
  34. app.use(function(request, result)
  35. {
  36. //prevents people from pointing their dns at my IP:port for my site
  37. if(request.headers.host.includes("localhost:" + port) ||
  38. request.headers.host.includes("jrtechs.net"))
  39. {
  40. const filename = url.parse(request.url, true).pathname;
  41. var project = false;
  42. projects.forEach(function(projectName)
  43. {
  44. if(filename.startsWith(projectName))
  45. {
  46. require("./sites/projects.js").main(request, result, projectName);
  47. project = true;
  48. }
  49. });
  50. if(filename.startsWith("/admin"))
  51. {
  52. require("./sites/admin.js").main(request, result, filename);
  53. project = true;
  54. }
  55. if(!project)
  56. {
  57. require("./sites/blog.js").main(request, result, filename);
  58. }
  59. try
  60. {
  61. const getClientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
  62. || request.connection.remoteAddress;
  63. console.log(getClientAddress);
  64. sql.logTraffic(getClientAddress, filename);
  65. }
  66. catch (e)
  67. { }
  68. }
  69. else
  70. {
  71. // utils.printWrongHost(result);
  72. result.writeHead(418, {});
  73. result.end();
  74. }
  75. });
  76. //enables gzip compression for the site
  77. app.use(compression());
  78. http.createServer(app).listen(port);