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.2 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 https = require('https');
  9. const url = require('url');
  10. var express = require("express");
  11. const fs = require('fs');
  12. var session = require('express-session');
  13. const includes = require('./includes/includes.js');
  14. const utils = require('./utils/utils.js');
  15. var map = require('./utils/generateSiteMap.js');
  16. map.main();
  17. var app = express();
  18. app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }}));
  19. var privateKey = fs.readFileSync('/etc/letsencrypt/live/jrtechs.net/privkey.pem');
  20. var certificate = fs.readFileSync('/etc/letsencrypt/live/jrtechs.net/cert.pem');
  21. var port = 8000;
  22. /**
  23. * Parses the request url and calls correct JS files
  24. */
  25. app.use(function(request, res)
  26. {
  27. var q = url.parse(request.url, true);
  28. var filename = q.pathname;
  29. //handles image requests
  30. if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png"))
  31. {
  32. require("./img/image.js").main(res, filename);
  33. }
  34. else if(filename.includes("/css/") || filename.includes(".txt"))
  35. {
  36. includes.sendCSS(res, filename)
  37. }
  38. else
  39. {
  40. var file = "";
  41. if(filename === '' || filename === '/')
  42. {
  43. file="./posts/homePage.js";
  44. }
  45. else
  46. {
  47. var urlSplit = filename.split("/");
  48. if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
  49. file = "./posts/category.js";
  50. else if(urlSplit.length >= 2 && urlSplit[1] === 'admin') //top secret admin page
  51. file = "./admin/admin.js";
  52. else
  53. file = "./posts/posts.js";
  54. }
  55. includes.printHeader(res).then(function()
  56. {
  57. return require(file).main(res, filename, request);
  58. }).then(function()
  59. {
  60. return includes.printFooter(res);
  61. }).catch(function(err)
  62. {
  63. console.log(err);
  64. })
  65. }
  66. });
  67. http.createServer(app).listen(port);