Express answers Cannot GET for a page that should exist
Cannot 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.
Fix it fast
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_modules2Match what you see, then jump to its fix
- 1
Cannot GET / on the home page, while other routes workNo GET route or static file exists for that pathFix - 2
Files 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
Cannot 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
Upgrade from Express 4, then a route with * or an inline regex stops matchingAn Express 5 path pattern that no longer matchesFix
- 1
What does the error look like?
Cannot GET /Cannot GET /api/usersCannot 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_modules2Fix
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
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.html2Fix
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.htmlConfirmed by expressjs.com
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_modules2Fix
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.js3Verify
Request the full prefixed path.
curl -i http://localhost:3000/api/usersConfirmed by expressjs.com
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_modules2Fix
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.pngConfirmed by expressjs.com
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
Back 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 fixBack end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fixBack end
Browser blocks a stylesheet or script: MIME type mismatch with nosniff
The Network tab shows the asset URL answered with Content-Type text/html
With X-Content-Type-Options: nosniff the browser accepts a stylesheet only as text/css and a script only as a JavaScript type.
3 causes, each with a fix