Convert express logic to fastify logic

This commit is contained in:
Raymonzut
2020-05-14 20:07:43 +02:00
parent 0dd0ac74e1
commit e9b9f01101
2 changed files with 21 additions and 13 deletions
+5 -6
View File
@@ -1,10 +1,9 @@
const express = require('express')
const fs = require('fs')
const router = express.Router()
const posts_dir = 'posts/'
const routes = new Map();
if (!fs.existsSync(posts_dir)) throw Error(`Missing ${posts_dir}`)
let posts = readPosts();
@@ -34,7 +33,7 @@ function notFoundResponse(res) {
res.status(404).send('Sorry, can not find that')
}
router.get('/', async (req, res) => {
routes.set('', async (req, res) => {
if (req.query.sort === '-1' || req.query.sort === '1') {
const posts_sorted = posts.sort((a, b) => {
return new Date(a.date).getTime() - new Date(b.date).getTime()
@@ -45,7 +44,7 @@ router.get('/', async (req, res) => {
}
})
router.get('/:id', async (req, res) => {
routes.set('/:id', async (req, res) => {
const re = /[0-9A-Fa-f]{24}/g
if (!re.test(req.params.id)) {
@@ -58,4 +57,4 @@ router.get('/:id', async (req, res) => {
else res.send(results[0])
})
module.exports = router
module.exports = routes