Rewrite posts route to use posts folder
- Remove mongodb related code - Implement file based posts
This commit is contained in:
+39
-21
@@ -1,43 +1,61 @@
|
|||||||
const express = require('express')
|
const express = require('express')
|
||||||
const mongodb = require('mongodb')
|
const fs = require('fs')
|
||||||
|
|
||||||
const router = express.Router()
|
const router = express.Router()
|
||||||
|
|
||||||
const DB_NAME = process.env.DB_NAME || 'test'
|
const posts_dir = 'posts/'
|
||||||
const URI = process.env.URI || 'mongodb://localhost:27017'
|
|
||||||
|
|
||||||
async function getPostCollection() {
|
if (!fs.existsSync(posts_dir)) throw Error(`Missing ${posts_dir}`)
|
||||||
const client = await mongodb.MongoClient.connect(URI, {
|
|
||||||
useNewUrlParser: true,
|
let posts = readPosts();
|
||||||
useUnifiedTopology: true,
|
|
||||||
})
|
setInterval(() => posts = readPosts(), 1000 * 60 * 60)
|
||||||
return client.db(DB_NAME).collection('posts')
|
|
||||||
|
function readPostsDelayed() {
|
||||||
|
return new Promise(setTimeout, 1000).then(posts = readPosts())
|
||||||
|
}
|
||||||
|
|
||||||
|
function readPosts() {
|
||||||
|
console.warn("reading all posts")
|
||||||
|
const files = fs.readdirSync(posts_dir)
|
||||||
|
|
||||||
|
if (files.length === 0) throw Error(`Could not find posts in ${posts_dir}`)
|
||||||
|
|
||||||
|
return files.filter(file_name => file_name.endsWith('.json'))
|
||||||
|
.map(file_name => `${posts_dir}${file_name}`)
|
||||||
|
.map(readJSONAsObject)
|
||||||
|
}
|
||||||
|
|
||||||
|
function readJSONAsObject(filename) {
|
||||||
|
return JSON.parse(fs.readFileSync(filename, 'utf8'))
|
||||||
|
}
|
||||||
|
|
||||||
|
function notFoundResponse(res) {
|
||||||
|
res.status(404).send('Sorry, can not find that')
|
||||||
}
|
}
|
||||||
|
|
||||||
router.get('/', async (req, res) => {
|
router.get('/', async (req, res) => {
|
||||||
const posts = await getPostCollection()
|
|
||||||
if (req.query.sort === '-1' || req.query.sort === '1') {
|
if (req.query.sort === '-1' || req.query.sort === '1') {
|
||||||
res.send(
|
const posts_sorted = posts.sort((a, b) => {
|
||||||
await posts
|
return new Date(a.date).getTime() - new Date(b.date).getTime()
|
||||||
.find({})
|
})
|
||||||
.sort({ date: parseInt(req.query.sort) })
|
|
||||||
.toArray()
|
if (req.query.sort === '1') res.send(posts_sorted)
|
||||||
)
|
else res.send(posts_sorted.reverse())
|
||||||
return
|
|
||||||
}
|
}
|
||||||
res.send(await posts.find({}).toArray())
|
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/:id', async (req, res) => {
|
router.get('/:id', async (req, res) => {
|
||||||
const re = /[0-9A-Fa-f]{24}/g
|
const re = /[0-9A-Fa-f]{24}/g
|
||||||
|
|
||||||
if (!re.test(req.params.id)) {
|
if (!re.test(req.params.id)) {
|
||||||
res.status(404).send('Sorry, can not find that')
|
notFoundResponse(res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
const results = posts.filter(post => post._id === req.params.id)
|
||||||
|
|
||||||
const posts = await getPostCollection()
|
if (!results.length) notFoundResponse(res)
|
||||||
res.send(await posts.find({ _id: mongodb.ObjectID(req.params.id) }).toArray())
|
else res.send(results[0])
|
||||||
})
|
})
|
||||||
|
|
||||||
module.exports = router
|
module.exports = router
|
||||||
|
|||||||
Reference in New Issue
Block a user