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.

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