Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

Cannot GET / in Express: why it happens and how to fix it

Cannot GET / is Express's default 404 page: the app is running, but no route or middleware answered a GET request for that path. Usually the route is missing or registered for another method, express.static points at a folder relative to the wrong directory, a router is mounted under a prefix, or the start command runs another file. Cannot GET /about after a deploy is a single-page app with no catch-all route, written /{*splat} in Express 5.

By Livops team, the people who build and run LivopsUpdated

What the browser shows, and what curl -i shows underneath, from Express 5:

HTTP/1.1 404 Not Found
X-Powered-By: Express
Content-Security-Policy: default-src 'none'
Content-Type: text/html; charset=utf-8

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /about</pre>
</body>
</html>

This page explains where that response comes from, the five reasons Express has nothing for the path you asked for, and the fix for each. The last section shows how it looks on Livops.

What does Cannot GET / mean?

It is a 404, not a crash. Express runs a request through its middleware and routes in order, and when none of them sends a response, it has nothing left to do[2]. Express then hands the request to its final handler[7], which sets status 404 and answers with Cannot , the request method and the path[8], in the small HTML page above[8].

So the message carries two clues:

  • The method. Cannot GET means a GET request. Cannot POST /api/login means no POST route matched, even if a GET route has the same path. A route is a method and a path together[1].
  • The path, without the query string: query strings are not part of the route path[1].

And one clue in the headers: X-Powered-By: Express, which Express adds unless you turned it off[7]. The answer came from your app, so the app is running and reachable. If the browser shows a 502 or a timeout instead, the problem is elsewhere: see 502 Bad Gateway.

Causes, most common first

1. There is no route for that path, or for that method

The app has app.get("/api/health", ...) and nothing for /. Nothing in Express answers / by default.

Fix: register the route, with the method the client really uses.

app.get("/", (req, res) => res.send("ok"));
app.post("/api/login", login);          // a form or fetch() that POSTs needs app.post

app.all() registers one handler for every HTTP method on a path[1].

2. express.static points at the wrong folder

express.static("public") serves files from public, and the folder name is not part of the URL: public/index.html is served at /[3], because index.html is the default directory index[4]. When the file is not found, express.static does not send a 404 itself; it calls next()[4], and if nothing after it answers, you get Cannot GET /.

The usual reason the file is not found: a relative folder is resolved from the directory where you launched the node process, not from the file that calls express.static[3]. node server.js from the project folder works; node app/server.js from one level up, or a process manager started elsewhere, looks for public in the wrong place.

Fix: give it an absolute path, as the Express docs advise[3].

// CommonJS
const path = require("path");
app.use(express.static(path.join(__dirname, "public")));
// ES module
import path from "node:path";
app.use(express.static(path.join(import.meta.dirname, "public")));

Two more things to check here:

  • A mount path changes the URL. With app.use("/static", express.static("public")), public/logo.png is at /static/logo.png[3], and / is not served from public at all.
  • The folder must exist on the server. If public/ or dist/ is created by a build step, the build must run before the app starts.

On Express 5, files and folders that start with a dot, such as .well-known, are ignored by default and return 404[6]; serve them with { dotfiles: "allow" }[6].

3. A single-page app has no catch-all route

The React, Vue or Angular app loads at /, and links inside it work. But a refresh on /about, or a link shared from it, gives Cannot GET /about. The client-side router handles /about in the browser; the server never had a file or a route for it.

Fix: after the static files and the API routes, send index.html for every other GET. res.sendFile needs an absolute path, unless you set its root option[5].

const path = require("path");
const dist = path.join(__dirname, "dist");

app.use("/api", api);                         // API routes first
app.use("/api", (req, res) => {               // an unknown API path stays a 404
  res.status(404).json({ error: "not found" });
});
app.use(express.static(dist));                // then real files
app.get("/{*splat}", (req, res) => {          // then everything else
  res.sendFile(path.join(dist, "index.html"));
});

The Express 5 syntax. Express 4 accepted app.get("*", ...). In Express 5 a wildcard must have a name: /*splat instead of /*, and /{*splat} to match the root path / as well[6]. Express matches paths with path-to-regexp[1], which requires a name after *[9], so a bare * stops the app at startup:

PathError [TypeError]: Missing parameter name at index 1: *; visit https://git.new/pathToRegexpError for info
  originalPath: '*'

Keep the API and its own 404 above the catch-all, as in the example: otherwise a mistyped API path gets your index.html with status 200. The same problem on static hosts, and its fixes there, is in 404 on page refresh in a React or Vue app.

4. A router is mounted under a prefix

app.use("/birds", birds) makes a router’s / answer at /birds and its /about at /birds/about[1]. The prefix is added once, by the mount. Writing it again inside the router doubles it:

// routes/users.js
router.get("/api/users", list);     // wrong: this answers /api/api/users
router.get("/users", list);         // right: this answers /api/users

// server.js
app.use("/api", require("./routes/users"));

Also check that the router is mounted at all: a routes/ file that nobody require()s or imports registers nothing.

5. The server runs a different file

The route is in server.js, but the server starts index.js, an older file that never registers it. Or start runs a dist/server.js that was committed to Git weeks ago and is not rebuilt on deploy. The app is up, answers with X-Powered-By: Express, and has none of your new routes.

Fix: make start in package.json name the file you edit, or the file the build writes:

"scripts": {
  "build": "tsc",
  "start": "node dist/server.js"
}

Log the routes at startup to see what really runs: one console.log("routes loaded") next to app.use("/api", api) tells you in the server’s logs whether that line ran.

Your own 404 page

Cannot GET is what Express sends when you have no 404 handler. A 404 is not an error to Express, so error-handling middleware does not catch it; add a normal middleware at the very bottom of the stack instead[2]:

app.use((req, res) => {
  res.status(404).send("Not found");
});

It answers every request that nothing above it answered, whatever the method.

How to confirm the fix locally

Start the app the way the server does, from another directory, and ask for the paths that failed:

cd /tmp && PORT=3000 node /path/to/project/server.js &
curl -i http://localhost:3000/
curl -i http://localhost:3000/about
curl -i -X POST http://localhost:3000/api/login

A 200 for / from /tmp proves express.static uses an absolute path; a 200 with index.html for /about proves the catch-all.

On Livops

  • The port. A web app must listen on the port in the PORT variable. An app that listens on localhost can be reached only from inside its container: listen on 0.0.0.0. See The app did not start.

    app.listen(process.env.PORT || 3000, "0.0.0.0");
  • The start command. Set it in Settings → Start, or with a web: line in a Procfile. Without one, Livops looks for the start script in package.json, then the main field, then the files index.js, server.js, app.js, main.js, in that order. A repository with an old index.js next to the real server.js starts index.js: cause 5. In livops.json, start is applied when the project is created; after that it is changed in the project’s settings. See No start command.

    {
      "build": "npm run build",
      "start": "node dist/server.js"
    }
  • The logs. An app that stops at startup, for example on the PathError from a bare *, fails the deploy as start_failed, and the Logs page shows its last words. A web app’s previous version keeps running meanwhile.

  • A single-page app without a server. If Express only serves a React or Vue build, you may not need Express at all. Deployed as a static site, a build with a detected client-side router gets index.html for page requests without a file extension, and a missing image or script still gets a 404. See Redirects and headers.

Running Express in production, with the port, the proxy and graceful shutdown, is in Express.js hosting.

Questions

Is Cannot GET / an error in my code?

No exception was thrown. It is the 404 page Express sends when every middleware and route ran and none of them answered. Your app is running; it just has nothing registered for that method and path.

Why does / work but refreshing /about gives Cannot GET /about?

The app is a single-page app. Clicking a link changes the address in the browser, but a refresh asks the server for /about, and Express has no route for it. Add a catch-all GET route after your static files and API routes that sends index.html.

How do I write a catch-all route in Express 5?

Give the wildcard a name: /*splat matches every path except the root, and /{*splat} matches the root too. A bare * worked in Express 4, but Express 5 stops at startup with Missing parameter name.

Why does express.static work locally but not on the server?

A relative folder such as public is resolved from the directory where the node process was started, not from your file. Locally you start it from the project folder; the server may not. Use path.join(__dirname, "public") or import.meta.dirname.

My route exists. Why do I still get Cannot GET?

Check three things. The method: Cannot POST means the route is registered with app.get only. The prefix: a router mounted with app.use("/api", router) adds /api to every path inside it. And the file: the start command may run an older entry file that never registers the route.

How do I replace the Cannot GET page with my own 404?

Add a middleware at the very end, after all routes: app.use((req, res) => res.status(404).send("Not found")). Express's own FAQ shows the same pattern. An error handler does not catch it, because a 404 is not an error in Express.

Does Livops need a catch-all for a React or Vue app?

Only if Express serves it. A static site deployed as a site, with a detected client-side router, gets index.html for page requests without a file extension, with no server code at all.

Sources

  1. Express — Routing (expressjs.com). Checked .
  2. Express — FAQ (expressjs.com). Checked .
  3. Express — Serving static files in Express (expressjs.com). Checked .
  4. Express 5.x API — express() (expressjs.com). Checked .
  5. Express 5.x API — Response (expressjs.com). Checked .
  6. Express — Migrating to Express 5 (expressjs.com). Checked .
  7. Express source — lib/application.js (github.com). Checked .
  8. finalhandler source — index.js (github.com). Checked .
  9. path-to-regexp — README (github.com). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your Express app

Got a question? — @livops_uz