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.6 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. /** Port for the server to run on */
  10. const port = config.PORT;
  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. /**session data for login */
  23. const session = require('express-session');
  24. /**Initializes sessions for login */
  25. app.use(session({ secret: config.SESSION_SECRET, cookie: { maxAge: 6000000 }}));
  26. const routes = require('./routes');
  27. app.use('/', routes);
  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. //
  39. // var project = false;
  40. // projects.forEach(function(projectName)
  41. // {
  42. // if(filename.startsWith(projectName))
  43. // {
  44. // require("./sites/projects.js").main(request, result, projectName);
  45. // project = true;
  46. // }
  47. // });
  48. //
  49. // if(filename.startsWith("/admin"))
  50. // {
  51. // require("./sites/admin.js").main(request, result, filename);
  52. // project = true;
  53. // }
  54. //
  55. // if(!project)
  56. // {
  57. // require("./sites/blog.js").main(request, result, filename);
  58. // }
  59. //
  60. // try
  61. // {
  62. // const getClientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
  63. // || request.connection.remoteAddress;
  64. // console.log(getClientAddress);
  65. //
  66. // sql.logTraffic(getClientAddress, filename);
  67. // }
  68. // catch (e)
  69. // { }
  70. // }
  71. // else
  72. // {
  73. // // utils.printWrongHost(result);
  74. // result.writeHead(418, {});
  75. // result.end();
  76. // }
  77. // });
  78. //enables gzip compression for the site
  79. app.use(compression());
  80. app.listen(port, () =>
  81. console.log(`App listening on port ${port}!`)
  82. );