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.

85 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. * @author Jeffery Russell 7-21-18
  8. */
  9. const http = require('http');
  10. const url = require('url');
  11. const express = require("express");
  12. const session = require('express-session');
  13. const includes = require('./includes/includes.js');
  14. const app = express();
  15. const utils = require('./utils/utils.js');
  16. const port = 8001;
  17. const cache = require('memory-cache');
  18. /** Initializes sessions for login */
  19. app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }}));
  20. /**
  21. * Parses the request url and calls correct JS files
  22. */
  23. app.use(function(request, res)
  24. {
  25. if(request.headers.host.includes("localhost:" + port) ||
  26. request.headers.host.includes("jrtechs.net"))
  27. {
  28. const filename = url.parse(request.url, true).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, cache);
  33. }
  34. else if(filename.includes("/css/") || filename.includes(".woff2"))
  35. {
  36. includes.sendCSS(res, filename, cache)
  37. }
  38. else if(filename.includes("/js/") || filename.includes(".js"))
  39. {
  40. require("./js/js.js").main(res, filename, cache);
  41. }
  42. else
  43. {
  44. res.writeHead(200, {'Content-Type': 'text/html'});
  45. file = "./admin/admin.js";
  46. Promise.all([includes.printAdminHeader(),
  47. require(file).main(filename, request),
  48. includes.printFooter()]).then(function(content)
  49. {
  50. res.write(content.join(''));
  51. res.end();
  52. }).catch(function(err)
  53. {
  54. console.log(err);
  55. throw err;
  56. });
  57. }
  58. }
  59. else
  60. {
  61. // utils.printWrongHost(res);
  62. res.writeHead(418, {});
  63. res.end();
  64. }
  65. });
  66. http.createServer(app).listen(port);