Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /Short answer
Cannot GET / is Express saying no route or file matches the request. For an Angular build it comes from serving the wrong folder, because ng build writes the site into dist/<project>/browser, or from having no fallback that returns index.html for client-side routes. Point express.static at the browser folder and add the fallback.
Fix it fast
1Find out which cause you have
Build the app and look at what the output folder really contains. The index.html you serve must be inside it.
ng build ls dist/*/browser | head2Match what you see, then jump to its fix
- 1
Cannot GET / even on the home pageexpress.static points at the wrong folderFix - 2
The home page works, but a route like /dashboard shows Cannot GET after a refreshDeep links and refreshes have no fallback to index.htmlFix - 3
ng build is set to outputMode static and index.html only holds the root pageThe build was prerendered, so the fallback file has a different nameFix
- 1
What does the error look like?
Cannot GET /Cannot GET /dashboard
Cause 1 of 3
express.static points at the wrong folder
1Check
ng build puts the files to deploy in a browser subfolder of the project's output folder, and static middleware only serves files below the path it was given.
ls dist/my-app/browser/index.html2Fix
Serve the browser folder, using an absolute path so the working directory of node does not matter.
const path = require('path');
const dir = path.join(__dirname, 'dist/my-app/browser');
app.use(express.static(dir));3Verify
The home page loads.
curl -I http://localhost:3000/Confirmed by angular.dev
Cause 2 of 3
Deep links and refreshes have no fallback to index.html
1Check
The Angular router lives in the browser. The server has no file for /dashboard and, with no other route, it answers Cannot GET.
curl -i http://localhost:3000/dashboard | head -n 52Fix
Angular's deployment guide asks you to configure the server fallback to index.html. With Express, add a last handler after express.static that sends the file for any other request.
app.use((req, res) => {
res.sendFile(path.join(dir, 'index.html'));
});3Verify
A refresh on /dashboard loads the app.
curl -I http://localhost:3000/dashboardConfirmed by angular.dev
Cause 3 of 3
The build was prerendered, so the fallback file has a different name
1Check
With static prerendering the browser folder includes index.csr.html next to prerendered pages.
ls dist/my-app/browser | grep index2Fix
Use index.csr.html as the fallback in the handler above.
res.sendFile(path.join(dir, 'index.csr.html'));3Verify
Deep links open without a blank page.
curl -I http://localhost:3000/dashboardConfirmed by angular.dev
How do I stop it coming back?
- Keep the output path in one place (angular.json) and read it in the server instead of copying it.
- Put API routes before the fallback handler so they are not answered with index.html.
- Test one deep link after every deploy, not only the home page.
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
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 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
Discord OAuth2 login fails on the redirect_uri in an Express app
invalid_request
Discord compares the redirect_uri you send with the URL you registered for the application, and the token step must repeat the same value as the authorization step.
4 causes, each with a fix