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.4 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. //http server
  10. const http = require('http');
  11. //parsing request url
  12. const url = require('url');
  13. //express app
  14. const express = require("express");
  15. const app = express();
  16. //session data for login
  17. const session = require('express-session');
  18. //sending static content
  19. const includes = require('./includes/includes.js');
  20. //used for file io
  21. const utils = require('./utils/utils.js');
  22. //cache -- only used for static contents
  23. const cache = require('memory-cache');
  24. //Initializes sessions for login
  25. app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }}));
  26. //port to listen for the admin server on
  27. const port = 8001;
  28. /**
  29. * Parses the request url and calls correct JS files
  30. */
  31. app.use(function(request, result)
  32. {
  33. //prevents people from pointing their dns at my IP:port for my site
  34. if(request.headers.host.includes("localhost:" + port) ||
  35. request.headers.host.includes("jrtechs.net"))
  36. {
  37. const filename = url.parse(request.url, true).pathname;
  38. //handles image requests
  39. if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png"))
  40. {
  41. includes.sendJS(result, filename, cache);
  42. }
  43. else if(filename.includes("/css/") || filename.includes(".woff2"))
  44. {
  45. includes.sendCSS(result, filename, cache)
  46. }
  47. else if(filename.includes("/js/") || filename.includes(".js"))
  48. {
  49. includes.sendJS(result, filename, cache);
  50. }
  51. else
  52. {
  53. result.writeHead(200, {'Content-Type': 'text/html'});
  54. const file = "./admin/admin.js";
  55. Promise.all([includes.printAdminHeader(),
  56. require(file).main(request),
  57. includes.printFooter()]).then(function(content)
  58. {
  59. result.write(content.join(''));
  60. result.end();
  61. }).catch(function(err)
  62. {
  63. console.log(err);
  64. throw err;
  65. });
  66. }
  67. }
  68. else
  69. {
  70. // utils.printWrongHost(result);
  71. result.writeHead(418, {});
  72. result.end();
  73. }
  74. });
  75. http.createServer(app).listen(port);