Skip to content

Commit

Permalink
getting post by Id
Browse files Browse the repository at this point in the history
  • Loading branch information
jayeshshaw committed Jul 2, 2023
1 parent c74d181 commit 294ea02
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions routes/api/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,25 @@ router.get('/', auth, async (req, res) => {
});


// @route GET api/posts/:id
// @desc Get post by ID
// @access Private
router.get('/:id', auth, async (req, res) => {
try {
const post = await Post.findById(req.params.id);

if (!post) {
return res.status(404).json({ msg: 'Post not found' });
}

res.json(post);
} catch (err) {
console.error(err.message);
if (err.kind=='ObjectId') {
return res.status(404).json({ msg: 'Post not found' });
}
res.status(500).send('Server Error');
}
});

module.exports= router;

0 comments on commit 294ea02

Please sign in to comment.