Express answers Cannot POST when the form is submitted
Cannot POST /submitShort answer
Cannot POST is Express's default answer when no route matches a POST to that exact path. Either the path only has a GET handler, or the form posts to a URL that differs from the route once a router prefix is added. Compare the form's action with the route, method and prefix included.
Fix it fast
1Find out which cause you have
List what Express has registered, then compare the exact path in the error with each route. The path in the message is the path the browser really requested.
grep -rn "app\.\(get\|post\|use\|all\)\|router\.\(get\|post\)" --include=*.js . | grep -v node_modules2Match what you see, then jump to its fix
- 1
Opening the same URL in the browser works, and only the form submit failsThe path has a GET route but no POST routeFix - 2
The route exists in a router file, but the error shows a path without the mount prefix, or the router is never mountedThe route lives in a router mounted under a prefix that the form leaves outFix
- 1
What does the error look like?
Cannot POST /submitCannot POST /api/users
Cause 1 of 2
The path has a GET route but no POST route
1Check
A route handles only the method it names. app.get('/submit') answers GET requests, and a POST to the same path falls through to the 404 reply.
grep -rn "'/submit'" --include=*.js . | grep -v node_modules2Fix
Add a handler with app.post for that path, or use app.all if the same function must serve several methods.
app.get('/submit', (req, res) => res.render('form'));
app.post('/submit', express.urlencoded({ extended: true }), (req, res) => {
res.send(`Got ${req.body.name}`);
});3Verify
Submit the form again. The response is the handler's output and not Cannot POST.
curl -i -X POST http://localhost:3000/submit -d 'name=Sam'Confirmed by expressjs.com
Cause 2 of 2
The route lives in a router mounted under a prefix that the form leaves out
1Check
A router mounted at /users serves router.post('/') as POST /users, and router.post('/new') as POST /users/new. If the router file is not mounted with app.use, none of its routes exist.
grep -rn "app.use(" --include=*.js . | grep -v node_modules2Fix
Make the form's action equal to the mount path plus the route path, and make sure the router is mounted.
// app.js
const users = require('./routes/users');
app.use('/users', users);
// routes/users.js
router.post('/new', (req, res) => res.redirect('/users'));
// form
// <form method="post" action="/users/new">3Verify
Post to the combined path and expect a redirect, not the error.
curl -i -X POST http://localhost:3000/users/newConfirmed by expressjs.com
How do I stop it coming back?
- Keep form actions and routes in one place (constants or a routes list), so they cannot drift apart.
- Add one request test per form that posts to its action and checks the status code.
- Add your own 404 handler that prints the method and path, to make mismatches visible in the logs.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Express answers Cannot GET for a page that should exist
Cannot GET /api/users
Cannot GET is Express's default reply when no route or static file matches the request.
4 causes, each with a fixServers
POST requests to a Node.js or Express API return 405 Not Allowed from nginx
The error page is served by nginx, and the Express logs show no request
When the 405 page comes from nginx and not from Express, nginx never handed the POST to your app.
3 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fix