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.

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