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.

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