My personal website https://leviolson.com
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.

115 lines
3.1 KiB

6 years ago
3 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. 'use strict'
  2. const express = require('express')
  3. const cors = require('cors')
  4. const fs = require('fs')
  5. const path = require('path')
  6. const app = express()
  7. let posts = []
  8. app.use(cors())
  9. app.use(express.static('public'))
  10. app.set('view engine', 'ejs')
  11. app.get('/', (req, res) => {
  12. res.render('pages/index', {
  13. title: "Levi Olson",
  14. active: "home",
  15. content: ""
  16. })
  17. })
  18. // This is really really inefficient... this should be cached and built during a build.
  19. app.get('/posts', (req, res) => {
  20. let data = {
  21. title: "Posts - Levi Olson",
  22. active: "posts",
  23. posts: posts
  24. }
  25. if (posts.length === 0) {
  26. const postDir = __dirname + '/posts'
  27. let files = fs.readdirSync(postDir, 'utf8')
  28. for (let i = 0; i < files.length; i++) {
  29. if (path.extname(files[i]) === '.json') {
  30. let postData = getData(files[i])
  31. if (postData && postData.active === 'posts') {
  32. data.posts.push(postData)
  33. } else {
  34. console.log(files[i], 'does not have a corresponding "html" file or is not active')
  35. }
  36. }
  37. }
  38. data.posts.sort(function (a, b) {
  39. console.log(new Date(a.created_at_short), new Date(b.created_at_short));
  40. var keyA = new Date(a.created_at_short),
  41. keyB = new Date(b.created_at_short)
  42. // Compare the 2 dates
  43. if (keyA < keyB) return 1
  44. if (keyA > keyB) return -1
  45. return 0
  46. })
  47. posts = data.posts
  48. }
  49. res.render('pages/posts', data)
  50. })
  51. function getData(file) {
  52. let postData
  53. try {
  54. postData = JSON.parse(fs.readFileSync('./posts/' + file, 'utf8'))
  55. postData.content = fs.readFileSync('./posts/' + postData.content_file, 'utf8')
  56. } catch (e) {
  57. return
  58. }
  59. return postData
  60. }
  61. app.get('/about', (req, res) => {
  62. res.sendFile(__dirname + '/views/pages/about.html')
  63. })
  64. app.get('/uncopyright', (req, res) => {
  65. return res.sendFile(__dirname + '/views/pages/uncopyright.html')
  66. })
  67. app.get('/404', (req, res) => {
  68. return res.status(404).render('pages/404', {
  69. title: "Page Not Found - Levi Olson",
  70. active: ""
  71. })
  72. })
  73. app.get('/core.css', (req, res) => {
  74. res.sendFile(__dirname + '/core.css')
  75. })
  76. app.get('/posts/:post', (req, res) => {
  77. let post = req.params.post
  78. let postData
  79. try {
  80. postData = JSON.parse(fs.readFileSync('./posts/' + post + '.json', 'utf8'))
  81. postData.content = fs.readFileSync('./posts/' + postData.content_file, 'utf8')
  82. } catch (e) {
  83. return res.redirect('/404')
  84. }
  85. return res.render('pages/post', postData)
  86. })
  87. app.get('/bnp/changelog', (req, res) => {
  88. let postData
  89. try {
  90. postData = JSON.parse(fs.readFileSync('./posts/bnp-changelog.json', 'utf8'))
  91. postData.content = fs.readFileSync('./posts/' + postData.content_file, 'utf8')
  92. } catch (e) {
  93. return res.redirect('/404')
  94. }
  95. return res.render('pages/post', postData)
  96. })
  97. const port = 3000
  98. app.listen(port, () => console.log('Example app listening on port ' + port + '!'))
  99. module.exports = app