Maaz Zindani

Loading the index…

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

Express answers Cannot GET for a page that should exist

ErrorCannot GET /

Short answer

Cannot GET is Express's default reply when no route or static file matches the request. Compare the exact URL with your routes: there may be no app.get for that path, the static folder is relative to the wrong directory, or a router is mounted under a prefix you left out of the URL.

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

Fix it fast

  1. 1Find out which cause you have

    Print every route Express knows, with its method, then compare one by one with the URL in the browser. Method, path and prefix must all match.

    curl -i http://localhost:3000/
    grep -rn "app\.\(get\|use\|all\)\|router\.\(get\|use\)" --include=*.js . | grep -v node_modules
  2. 2Match what you see, then jump to its fix

    1. 1Cannot GET / on the home page, while other routes workNo GET route or static file exists for that pathFix
    2. 2Files exist in the public folder, but every file URL gives Cannot GET, and it works when you start node from the project rootexpress.static is resolved from a different working directoryFix
    3. 3Cannot GET /users while /api/users works, or Cannot GET /css/site.css while /static/css/site.css worksA router or static folder is mounted under a prefixFix
    4. 4Upgrade from Express 4, then a route with * or an inline regex stops matchingAn Express 5 path pattern that no longer matchesFix

What does the error look like?

  • Cannot GET /
  • Cannot GET /api/users
  • Cannot GET /index.html

Cause 1 of 4

No GET route or static file exists for that path

1Check

A route only answers the HTTP method it was written for. A route written with app.post will not answer a browser visit, and a route that only exists for /home does not answer /.

grep -rn "app\.\(get\|post\|all\)(" --include=*.js . | grep -v node_modules

2Fix

Add a GET handler for the path, or serve a folder that contains an index.html.

app.get('/', (req, res) => {
  res.send('home');
});

3Verify

The page returns 200.

curl -i http://localhost:3000/

Confirmed by expressjs.com

Did this fix it?

Cause 2 of 4

express.static is resolved from a different working directory

1Check

A relative path in express.static is resolved from the directory where node was launched, not from the file that holds the code. A process manager or a different cwd changes it.

pwd
ls public/index.html

2Fix

Build an absolute path from the script's own location.

const path = require('path');
app.use(express.static(path.join(__dirname, 'public')));

3Verify

Start node from another folder and request a file.

cd /tmp && node /path/to/project/server.js &
curl -I http://localhost:3000/index.html

Confirmed by expressjs.com

Did this fix it?

Cause 3 of 4

A router or static folder is mounted under a prefix

1Check

A router mounted with app.use('/api', router) answers paths below /api only, and router.get('/') answers /api itself. A static mount path becomes a URL prefix, and the folder name is not part of the URL.

grep -rn "app\.use('/" --include=*.js . | grep -v node_modules

2Fix

Request the prefixed URL, or change the mount path so the real URL matches.

app.use('/api', usersRouter);          // GET /api/users
app.use('/static', express.static(dir)); // GET /static/app.js

3Verify

Request the full prefixed path.

curl -i http://localhost:3000/api/users

Confirmed by expressjs.com

Did this fix it?

Cause 4 of 4

An Express 5 path pattern that no longer matches

1Check

Express 5 uses path-to-regexp v8: a wildcard must have a name, and regex characters inside a string path are not supported.

grep -rn "get('\*'\|:[a-zA-Z]*(" --include=*.js . | grep -v node_modules

2Fix

Name the wildcard, use {*splat} to include the root, and use an array of paths or a real RegExp for restricted parameters.

app.get('/files/*filepath', handler);
app.get('/{*splat}', fallback);

3Verify

Request a path the pattern should catch.

curl -i http://localhost:3000/files/a/b.png

Confirmed by expressjs.com

Did this fix it?

How do I stop it coming back?

  • Use absolute paths for static folders and for sendFile.
  • Put routers in their own files and write the mount prefix once, in a single README line or a constant.
  • Add a catch-all 404 handler at the end that logs the method and URL so a missing route shows up in logs.
  • Read the Express 5 migration notes before upgrading routes that use wildcards.

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.