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.

82 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. var session = require('express-session');
  11. const includes = require('./includes/includes.js');
  12. //
  13. const utils = require('./utils/utils.js');
  14. //
  15. // var forceSsl = require('express-force-ssl');
  16. var map = require('./utils/generateSiteMap.js');
  17. map.main();
  18. var app = express();
  19. app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }}));
  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 if(filename.includes(/download/))
  34. // {
  35. // require("./downloads/downloads.js").main(res, filename, request);
  36. // }
  37. else
  38. {
  39. var file = "";
  40. if(filename === '' || filename === '/')
  41. filename = '/programming/using-english-conventions-to-write-clean-code/';
  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. includes.printHeader(res).then(function()
  50. {
  51. return require(file).main(res, filename, request);
  52. }).then(function()
  53. {
  54. return includes.printFooter(res);
  55. }).catch(function(err)
  56. {
  57. console.log(err);
  58. })
  59. }
  60. });
  61. //https.createServer(options, app).listen(443);
  62. http.createServer(app).listen(8080);
  63. //app.use(forceSsl);