Missing POST route error is not in the error log
I have a NodeJS server and I forgot to add a route for POST on a page.
I have a GET route, in routes/public.js
:
/**
* GET /festas
*
*
*/
router.get("/festas", async (req, res) => {
return res.render('festas', {
user: req.user,
text: TEXT, // equivalent of a global variable
});
});
router.get("/:token", async (req, res) => {
// Verifications, such as typos on the requested route,
// not shown here.
return res.redirect(301, "/404");
}
module.exports = router;
This module is called by app.js
:
app.use('/', require('./routes/public'));
and a PUG template:
.feedback
h2 #{text.feedback.title}
p #{text.feedback.body}
.container
form#commentForm(method="POST")
.comment
p #{text.feedback.comments}
input(type="text", name="commentString")
input.primary(type="submit", value="Submit")
When I click on the Submit button on the page, I see:
Cannot POST /festas
But I could not find this error in the error log. I would like to include this error in the error log so I catch this bug quickly in the future.
How can I add missing POST routes to error logging?
Solution 1:
I'm assuming you are express server and doing something like this:
// router.js
router.get("/festas", async (req, res) => {
return res.render("festas", {
user: req.user,
text: TEXT, // equivalent of a global variable
});
});
// other routes, GET and POST.
export default router;
// App.js
app.use('/', router);
app.post('*', (req, res) => {
// All unhandled POST routes end up here.
console.error("Unhandled POST route: " + req.originalUrl);
res.send('Not Found', 404);
});