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.

67 lines
1.6 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. var app = express();
  13. app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 6000000 }}));
  14. app.use(function(request, res)
  15. {
  16. var q = url.parse(request.url, true);
  17. var filename = q.pathname;
  18. //handles image requests
  19. if(filename.includes("/img/"))
  20. {
  21. require("./img/image.js").main(res, filename);
  22. }
  23. else if(filename.includes("/css/"))
  24. {
  25. includes.sendCSS(res, filename)
  26. }
  27. else
  28. {
  29. var file = "";
  30. if(filename === '' || filename === '/')
  31. filename = '/category/Java';
  32. var urlSplit = filename.split("/");
  33. if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
  34. file = "./posts/category.js";
  35. else if(urlSplit.length >= 2 && urlSplit[1] === 'admin') //top secret admin page
  36. file = "./admin/admin.js";
  37. else
  38. file = "./posts/posts.js";
  39. includes.printHeader(res).then(function()
  40. {
  41. return require(file).main(res, filename, request);
  42. }).then(function()
  43. {
  44. return includes.printFooter(res);
  45. }).then(function()
  46. {
  47. //console.log("fin"); //for debugging
  48. })
  49. }
  50. });
  51. http.createServer(app).listen(8080);