From d0bfcb2311e8a3b61acd20a46e46d40fcbf6d253 Mon Sep 17 00:00:00 2001 From: Levi Olson Date: Thu, 28 Jan 2021 09:04:08 -0600 Subject: [PATCH] fixing a stupid null vs array issue --- app.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app.js b/app.js index e9757d4..21963b0 100644 --- a/app.js +++ b/app.js @@ -6,7 +6,7 @@ const fs = require('fs') const path = require('path') const app = express() -let posts = null +let posts = [] app.use(cors()) app.use(express.static('public')) @@ -23,14 +23,15 @@ app.get('/', (req, res) => { // This is really really inefficient... this should be cached and built during a build. app.get('/posts', (req, res) => { - const postDir = __dirname + '/posts' - let files = fs.readdirSync(postDir, 'utf8') let data = { title: "Posts - Levi Olson", active: "posts", posts: posts } - if (!data.posts) { + if (posts.length === 0) { + const postDir = __dirname + '/posts' + let files = fs.readdirSync(postDir, 'utf8') + for (let i = 0; i < files.length; i++) { if (path.extname(files[i]) === '.json') { let postData = getData(files[i])