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.

101 lines
2.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. // Stores the configuration for the server
  8. const config = require('./utils/configLoader').getConfig();
  9. //http server
  10. const http = require('http');
  11. //used to parse the request URL
  12. const url = require('url');
  13. //express app
  14. const express = require("express");
  15. //express app
  16. const app = express();
  17. //server side logging
  18. const sql = require('./utils/sql');
  19. //Used for gzip compression
  20. const compression = require('compression');
  21. //used for file io
  22. const utils = require('./utils/utils.js');
  23. //Updates the site map whenever the server is started
  24. const map = require('./utils/generateSiteMap.js');
  25. map.main();
  26. //session data for login
  27. const session = require('express-session');
  28. //Initializes sessions for login
  29. app.use(session({ secret: config.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(config.PORT);