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.

87 lines
2.1 KiB

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.set('view engine', 'ejs')
  8. app.get('/', (req, res) => {
  9. res.render('pages/index', {
  10. title: "Levi Olson",
  11. active: "home",
  12. content: ""
  13. })
  14. })
  15. app.get('/posts', (req, res) => {
  16. const postDir = __dirname + '/posts'
  17. let files = fs.readdirSync(postDir, 'utf8')
  18. let data = {
  19. title: "Posts - Levi Olson",
  20. active: "posts",
  21. posts: []
  22. }
  23. for (let i = 0; i < files.length; i++) {
  24. if (path.extname(files[i]) === '.json') {
  25. let postData = getData(files[i])
  26. if (postData) {
  27. data.posts.push(postData)
  28. } else {
  29. console.log(files[i], 'does not have a corresponding "html" file')
  30. }
  31. }
  32. }
  33. res.render('pages/posts', data)
  34. })
  35. function getData(file) {
  36. let postData
  37. try {
  38. postData = JSON.parse(fs.readFileSync('./posts/' + file, 'utf8'))
  39. postData.content = fs.readFileSync('./posts/' + postData.content_file, 'utf8')
  40. } catch (e) {
  41. return
  42. }
  43. return postData
  44. }
  45. app.get('/about', (req, res) => {
  46. res.sendFile(__dirname + '/views/pages/about.html')
  47. })
  48. app.get('/uncopyright', (req, res) => {
  49. return res.sendFile(__dirname + '/views/pages/uncopyright.html')
  50. })
  51. app.get('/404', (req, res) => {
  52. return res.status(404).render('pages/404', {
  53. title: "Page Not Found - Levi Olson",
  54. active: ""
  55. })
  56. })
  57. app.get('/core.css', (req, res) => {
  58. res.sendFile(__dirname + '/core.css')
  59. })
  60. app.get('/posts/:post', (req, res) => {
  61. let post = req.params.post
  62. let postData
  63. try {
  64. postData = JSON.parse(fs.readFileSync('./posts/' + post + '.json', 'utf8'))
  65. postData.content = fs.readFileSync('./posts/' + postData.content_file, 'utf8')
  66. } catch (e) {
  67. return res.redirect('/404')
  68. }
  69. return res.render('pages/post', postData)
  70. })
  71. const port = 3000
  72. app.listen(port, () => console.log('Example app listening on port ' + port + '!'))
  73. module.exports = app