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.

88 lines
2.1 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 if(filename.includes("/downloads/"))
  36. {
  37. require("./downloads/downloads.js").main(res, filename, request);
  38. }
  39. else
  40. {
  41. var file = "";
  42. if(filename === '' || filename === '/')
  43. {
  44. file="./posts/homePage.js";
  45. }
  46. else
  47. {
  48. var urlSplit = filename.split("/");
  49. if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
  50. file = "./posts/category.js";
  51. else if(urlSplit.length >= 2 && urlSplit[1] === 'admin') //top secret admin page
  52. file = "./admin/admin.js";
  53. else
  54. file = "./posts/posts.js";
  55. }
  56. includes.printHeader(res).then(function()
  57. {
  58. return require(file).main(res, filename, request);
  59. }).then(function()
  60. {
  61. return includes.printFooter(res);
  62. }).catch(function(err)
  63. {
  64. console.log(err);
  65. })
  66. }
  67. });
  68. http.createServer(app).listen(port);