All files app.js

100% Statements 42/42
100% Branches 4/4
100% Functions 9/9
100% Lines 41/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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