Handlebars表单POST请求返回404错误排查求助
Hey there, let's work through why you're hitting that 404 error even with a valid contact ID!
The first thing that jumps out is a tiny but critical mistake in your Express route configuration: your POST route path is missing the leading slash /.
In Express, routes without a leading slash are treated as relative paths, which means your server isn't matching the POST request to /contacts/34634234 with your handler. That's exactly why you're getting a 404 error.
Fix the Route Path
Update your Node route code to include the leading slash:
router.post('/contacts/:id', function(req, res) { res.render('index'); });
Quick Additional Checks (Just to Be Thorough)
- Double-check that your Handlebars template is rendering the form's
actioncorrectly. View the page source in your browser to confirm it shows something like<form method="POST" action="/contacts/34634234">(no typos in the ID or path). - Make sure this router is properly mounted to your Express app (e.g., via
app.use('/', yourRouter)— if you mounted it under a different path like/api, you'd need to adjust your form's action accordingly, but based on your description this probably isn't the case).
Since you confirmed res.render('index') works elsewhere, fixing that leading slash in the route should resolve the 404 issue right away!
内容的提问来源于stack exchange,提问作者MickeyTheMouse

