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.

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