My ham website
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.

113 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 = null
  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. const postDir = __dirname + '/posts'
  21. let files = fs.readdirSync(postDir, 'utf8')
  22. let data = {
  23. title: "Posts - Levi Olson",
  24. active: "posts",
  25. posts: posts
  26. }
  27. if (!data.posts) {
  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. }
  48. res.render('pages/posts', data)
  49. })
  50. function getData(file) {
  51. let postData
  52. try {
  53. postData = JSON.parse(fs.readFileSync('./posts/' + file, 'utf8'))
  54. postData.content = fs.readFileSync('./posts/' + postData.content_file, 'utf8')
  55. } catch (e) {
  56. return
  57. }
  58. return postData
  59. }
  60. app.get('/about', (req, res) => {
  61. res.sendFile(__dirname + '/views/pages/about.html')
  62. })
  63. app.get('/uncopyright', (req, res) => {
  64. return res.sendFile(__dirname + '/views/pages/uncopyright.html')
  65. })
  66. app.get('/404', (req, res) => {
  67. return res.status(404).render('pages/404', {
  68. title: "Page Not Found - Levi Olson",
  69. active: ""
  70. })
  71. })
  72. app.get('/core.css', (req, res) => {
  73. res.sendFile(__dirname + '/core.css')
  74. })
  75. app.get('/posts/:post', (req, res) => {
  76. let post = req.params.post
  77. let postData
  78. try {
  79. postData = JSON.parse(fs.readFileSync('./posts/' + post + '.json', 'utf8'))
  80. postData.content = fs.readFileSync('./posts/' + postData.content_file, 'utf8')
  81. } catch (e) {
  82. return res.redirect('/404')
  83. }
  84. return res.render('pages/post', postData)
  85. })
  86. app.get('/bnp/changelog', (req, res) => {
  87. let postData
  88. try {
  89. postData = JSON.parse(fs.readFileSync('./posts/bnp-changelog.json', 'utf8'))
  90. postData.content = fs.readFileSync('./posts/' + postData.content_file, 'utf8')
  91. } catch (e) {
  92. return res.redirect('/404')
  93. }
  94. return res.render('pages/post', postData)
  95. })
  96. const port = 3000
  97. app.listen(port, () => console.log('Example app listening on port ' + port + '!'))
  98. module.exports = app