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.

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