フレームワーク
Express
Node.js 向けの最小構成の Web アプリケーションフレームワーク。ルーティングとミドルウェアを組み合わせて API サーバーや Web アプリを組み立てる。
バージョン移行
メジャーバージョンを上げたときに動かなくなることを、旧版と新版で同じコマンドを打ち比べて確かめた。
Express 4 → 5・変更点 9 つ
Express を 4 から 5 に上げると動かなくなること
問題一覧(8)
Express で「Cannot GET /パス」と表示されルートにアクセスできない
「Cannot GET /パス」は、メソッドとパスに一致するルートが無いときに Express が返す 404。POST のルートをブラウザで開いた、Router のマウントパスの二重指定、express.static の相対パスのずれ、SPA のパスの直接表示が主な原因だ。
更新: 2026-10-26、タグ: routing、404、express.static
Express「Cannot set headers after they are sent to the client」の原因と直し方
Express の Cannot set headers after they are sent to the client は、1 つのリクエストに 2 回応答すると出る。応答した直後に return するか、res.headersSent で送信済みかを確かめれば直る。
更新: 2026-11-07、タグ: nodejs、response、middleware
Express「Invalid status code ... Status code must be an integer」の原因と直し方
Express 5 の Invalid status code: "201". Status code must be an integer. は、res.status() に整数以外を渡すと出る。数値で渡し、res.redirect() は (status, url) の順に書けば直る。
更新: 2026-11-11、タグ: express5、res.status、res.redirect
Express 5 で「Missing parameter name」が出てサーバーが起動できない
Missing parameter name は、/* を /*splat、/:id? を {/:id} に書き換えれば直る。Express 5 の path-to-regexp 8 は、名前の無い * や ? を含むパスを登録時に例外にする。
更新: 2026-10-27、タグ: express5、routing、wildcard
Express で req.body が undefined になり POST の値を受け取れない
req.body はボディを解析するミドルウェアを通ったときだけ値が入る。express.json() の登録漏れや登録順、Content-Type の不一致、multipart の送信が主な原因で、Express 5 では解析されないと {} ではなく undefined になる。
更新: 2026-09-30、タグ: req.body、express.json、body-parser
Express 5「req.param is not a function」の原因と直し方
Express 5 の TypeError: req.param is not a function は、5 で消えた req.param() を呼ぶと出る。値の出どころに合わせて req.params・req.body・req.query から読めば直る。
更新: 2026-11-13、タグ: express5、req.param、sendfile
Express 5「Cannot set property query ... which has only a getter」の原因と直し方
Cannot set property query of #<IncomingMessage> which has only a getter は、Express 5 で req.query に代入するミドルウェアが出す。その前で req.query を書き込めるプロパティに置き換えれば直る。
更新: 2026-11-07、タグ: express5、req.query、express-mongo-sanitize
Express「Router.use() requires a middleware function」の原因と直し方
Router.use() requires a middleware function は、ルーターのファイルに module.exports = router を書けば直ることが多い。Express 5 では argument handler must be a function と出る。
更新: 2026-11-03、タグ: router、middleware、module-exports