できない.dev

Express で「Cannot GET /パス」と表示されルートにアクセスできない

「Cannot GET /パス」は、メソッドとパスに一致するルートが無いときに Express が返す 404。
POST のルートをブラウザで開いた、Router のマウントパスの二重指定、express.static の相対パスのずれ、SPA のパスの直接表示が主な原因だ。

公開: 更新:

症状できない
curl -s http://localhost:3000/users
…
<title>Error</title>
</head>
<body>
<pre>Cannot GET /users</pre>

2026-10-26 に node:20(Express 5.3.0 / Node.js 20.20.2) で実際に打って取った出力。検証環境の詳細

要約

ブラウザに次の 1 行だけが表示されるのは、Express がすべてのミドルウェアとルートを順に試し、どれも応答しなかったときに返す既定の 404 だ。

Cannot GET /about

公式 FAQ(新しいタブで開く) のとおり、Express では 404 はエラーとして扱われないので、エラー処理のミドルウェアには届かない。
表示の GET はメソッド、その後ろは Express が受け取ったパスなので、まずこの 2 つを登録したルートと突き合わせる。

実行例

node:20 のコンテナで Express 5.3.0 を起動すると、app.post だけのルート /users を GET で開いたときは Cannot GET /users が返り、POST では 200 が返った。
マウントパスを重ねたルーターは /api/api/items で [] を返し、/api/items は Cannot GET になった。

$ node src/server.js > server.log 2>&1 &
$ curl -s http://localhost:3000/users
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /users</pre>
</body>
</html>
$ curl -s -i -X POST http://localhost:3000/users
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 11
ETag: W/"b-Ai2R8hgEarLmHKwesT1qcY913ys"
Date: Mon, 26 Oct 2026 01:07:49 GMT
Connection: keep-alive
Keep-Alive: timeout=5
 
{"ok":true}
$ curl -s http://localhost:3000/api/items
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /api/items</pre>
</body>
</html>
$ curl -s http://localhost:3000/api/api/items
[]
$ curl -s http://localhost:3000/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /index.html</pre>
</body>
</html>
$ curl -s http://localhost:3000/about
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /about</pre>
</body>
</html>
$ node src/server.js > server.log 2>&1 &
$ curl -s http://localhost:3000/api/items
[]
$ curl -s http://localhost:3000/index.html
<!doctype html>
<title>spa</title>
<div id="root"></div>
$ curl -s http://localhost:3000/about
<!doctype html>
<title>spa</title>
<div id="root"></div>
$ curl -s http://localhost:3000/api/unknown
{"error":"not found"}

— 2026-10-26 時点の出力

検証環境Node.js 20.20.2 / npm 10.8.2 / Python 3.11.2 / 2026-10-26 検証(ほか 2 件)
検証日
実行環境
node:20 Debian GNU/Linux 12 (bookworm)
バージョン
  • Node.js 20.20.2
  • npm 10.8.2
  • Python 3.11.2
  • Git 2.39.5
  • Express 5.3.0

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. メソッドとパスを確かめる

curl -i -X POST http://localhost:3000/users

POST のルートは、curl や fetch() で POST を送って確かめる。

2. ルーターのパスはマウントパスを除いて書く

const router = express.Router();
router.get("/items", (req, res) => res.json([])); // /api/items で応答する
 
app.use("/api", router);

3. 静的ファイルは絶対パスで指定する

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

__dirname はそのファイルがあるディレクトリなので、起動した場所に左右されない。
ES Modules で __dirname が使えないときの書き方は、Node.js の ESM で __dirname が使えない場合の対処にまとめてある。

4. SPA はフォールバックで index.html を返す

const path = require("path");
const dist = path.join(__dirname, "dist");
 
apiRouter.use((req, res) => res.status(404).json({ error: "not found" })); // API の 404
app.use("/api", apiRouter); // API を先に登録する
app.use(express.static(dist));
app.get("/{*splat}", (req, res) => {
  res.sendFile(path.join(dist, "index.html"));
});

Express 5 ではパスに名前の無い * を書くと起動時に例外になるので、トップの / も含めてすべてに一致させるには /{*splat} と書く(Express 4 では *)。
フォールバックは存在しない /api/ のパスにも index.html を返すので、API のルーターの末尾には 404 を返すミドルウェアを置いておく。

よくある原因

  1. メソッドが違う。
    app.post('/users', ...) だけを定義していると、ブラウザで /users を開いたときは GET で届くので、Cannot GET /users になる。
  2. マウントパスが二重になっている。
    app.use('/api', router) でマウントしたルーターの中で router.get('/api/items', ...) と書くと、実際の URL は /api/api/items になり、/api/items は 404 になる。
    ルーターのパスは、マウントパスを取り除いた残りと照合される。
  3. 静的ファイルのディレクトリがずれている。
    公式ガイド(新しいタブで開く)にあるとおり、express.static('public') の相対パスは、サーバーのファイルの場所ではなく node を起動したディレクトリを基準に解決される。
    プロジェクトのルートで node src/server.js と起動すると、探すのは ./public で、src/public は見ない。
    ファイルが無いと express.static は次の処理へ渡すので、最後に Cannot GET が返る。
  4. SPA のパスを直接開いている。
    React Router や Vue Router のパスはブラウザの中で切り替わるだけで、サーバー側にルートは無い。
    トップから遷移すると表示できるのに、/about をリロードすると Cannot GET /about になる。