Maaz Zindani

Loading the index…

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

Angular app shows 'Cannot GET /' when served by a Node.js server

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

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

Fix it fast

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

    1. 1Cannot GET / even on the home pageexpress.static points at the wrong folderFix
    2. 2The home page works, but a route like /dashboard shows Cannot GET after a refreshDeep links and refreshes have no fallback to index.htmlFix
    3. 3ng 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

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

2Fix

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

Did this fix it?

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 5

2Fix

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

Confirmed by angular.dev

Did this fix it?

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 index

2Fix

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

Confirmed by angular.dev

Did this fix it?

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

Next step

Want this handled for good?

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