I'm really just looking for a way to clean up my code here. I have several routes like this, note that each route individually goes through the JSON web token verification process.
router.route('/some-route').post((req, res) => {
jwt.verify(req.body.token, secret, (err, decoded) => {
if (err) return console.log(err);
// do something
res.json({ some: 'response data' });
});
});
router.route('/some-other-route').post((req, res) => {
jwt.verify(req.body.token, secret, (err, decoded) => {
if (err) return console.log(err);
// do something else
res.json({ some: 'response data' });
});
});
Is there some other way? Some of my routes are approaching callback-hell levels of nesting, so it would be nice to eliminate this from every route.
Pretty sure you can tell the router to
usethe function as middleware. The function will be called for every route inrouter.