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.

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