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.

87 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. var https = require('https');
  9. const url = require('url');
  10. var express = require("express");
  11. var session = require('express-session');
  12. const includes = require('./includes/includes.js');
  13. var forceSsl = require('express-force-ssl');
  14. var app = express();
  15. var fs = require('fs');
  16. //var key = fs.readFileSync('private.key');
  17. //var cert = fs.readFileSync( 'primary.crt' );
  18. //var ca = fs.readFileSync( 'encryption/intermediate.crt' );
  19. // var options = {
  20. // key: key,
  21. // cert: cert,
  22. // ca: ca
  23. // };
  24. //the secret is different on production
  25. app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 6000000 }}));
  26. app.use(function(request, res)
  27. {
  28. var q = url.parse(request.url, true);
  29. var filename = q.pathname;
  30. //handles image requests
  31. if(filename.includes("/img/") || filename.includes(".jpg"))
  32. {
  33. require("./img/image.js").main(res, filename);
  34. }
  35. else if(filename.includes("/css/"))
  36. {
  37. includes.sendCSS(res, filename)
  38. }
  39. else
  40. {
  41. var file = "";
  42. if(filename === '' || filename === '/')
  43. filename = '/category/projects';
  44. var urlSplit = filename.split("/");
  45. if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
  46. file = "./posts/category.js";
  47. else if(urlSplit.length >= 2 && urlSplit[1] === 'admin') //top secret admin page
  48. file = "./admin/admin.js";
  49. else
  50. file = "./posts/posts.js";
  51. includes.printHeader(res).then(function()
  52. {
  53. return require(file).main(res, filename, request);
  54. }).then(function()
  55. {
  56. return includes.printFooter(res);
  57. }).then(function()
  58. {
  59. //console.log("fin"); //for debugging
  60. })
  61. }
  62. });
  63. //https.createServer(options, app).listen(443);
  64. http.createServer(app).listen(80);
  65. //app.use(forceSsl);