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.

103 lines
2.7 KiB

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