Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Express answers Cannot POST when the form is submitted

ErrorCannot POST /submit

Short 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.

  • Applies to Express 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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_modules
  2. 2Match what you see, then jump to its fix

    1. 1Opening the same URL in the browser works, and only the form submit failsThe path has a GET route but no POST routeFix
    2. 2The 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

What does the error look like?

  • Cannot POST /submit
  • Cannot 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_modules

2Fix

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

Did this fix it?

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_modules

2Fix

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/new

Confirmed by expressjs.com

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.