Express says Cannot GET or returns 404 on every route: the causes

The message Cannot GET / is Express saying: “I received a GET request for this path and none of my routes or files answers it”. The good news: the application is running (if it were not, you would see a 503), so the problem is routing, not the server. The short answer: there is no route for that path, or the static files folder points to the wrong place, or what is opening is not your application.

Finding which one, in order

1 Confirm it is your Express answering. Add a test route, restart the application and open /ping:app.get("/ping", (req, res) => res.send("ok"));If it answers “ok”, the server is fine and the fault is in your routes. If not, you are looking at something else: an index.html in the public folder that wins over the address, or another application. Check the address on the “Setup Node.js App” screen.
2 See what arrives. Put this before all routes and look at the log (where to read the logs):app.use((req, res, next) => {
  console.log(req.method, req.url);
  next();
});
3 Compare with the routes. The method, the full path and the prefix must match what is registered.
4 Restart. A Node application keeps the old code in memory until it is restarted: use the button on the application screen.

The most common causes

Cause How to recognise it Fix
No route for / Only / fails; /ping works. Define app.get("/", ...) or serve a folder with an index.html through express.static.
Wrong static folder express.static("public") works on your computer and not here. The relative path depends on where Node was started. Use path.join(__dirname, "public").
Route prefix The router is mounted at /api and you open /users. Open /api/users, or mount the router on the right prefix.
Different method The message says Cannot POST /x. The route is defined with get and the form sends post, or the other way round.
Route order A catch-all (*) or a 404 handler sits before the others. Put them last: Express uses the first route that matches.
Capital letters in files The file exists, but the address asks for another name. Express ignores case in routes by default, but file names in a static folder are case-sensitive.
Old code in memory The fix is done and the error does not change. Restart the application.
Do not “fix” it by hard-coding or opening a port. On shared hosting the application is served through the domain, by Passenger, not by a port number. Keep process.env.PORT || 3000 in listen, which works here and on your computer. If the application will not even start, that is another case: a 503 on a Node.js app.
Write a 404 that helps. After all the routes, a handler that returns the method and path it received saves a lot of time: app.use((req, res) => res.status(404).send(req.method + " " + req.url + " not found")). Look at the result and compare it with what you expected.

The route exists, the log shows the request and it still fails? Send us the domain and the last lines of the log.

Open a support ticket

SEE ALSO

Getting a Node.js application running

Node.js app in cPanel returns 503: the usual causes

Where console.log and print output goes, and how to read your app logs

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from 321,75 MT/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?