From 74185beb79165f51f89e8ca945ef68b0f118f14d Mon Sep 17 00:00:00 2001 From: Raymonzut <40148684+Raymonzut@users.noreply.github.com> Date: Tue, 21 Apr 2020 12:32:55 +0200 Subject: [PATCH] Add sort option for posts route / --- server/routes/api/posts.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/server/routes/api/posts.js b/server/routes/api/posts.js index 45e8c18..e7939ac 100644 --- a/server/routes/api/posts.js +++ b/server/routes/api/posts.js @@ -16,6 +16,15 @@ async function getPostCollection() { router.get('/', async (req, res) => { const posts = await getPostCollection() + if (req.query.sort === '-1' || req.query.sort === '1') { + res.send( + await posts + .find({}) + .sort({ date: parseInt(req.query.sort) }) + .toArray() + ) + return + } res.send(await posts.find({}).toArray()) })