Express を 4 から 5 に上げると動かなくなること
Express を 4 から 5 に上げると、名前の無い * や :id? のルートが起動の時点で PathError になり、req.param() や req.query への代入は TypeError で落ちる。
4.22.3 と 5.3.0 で打ち比べた 9 つの変更点と直し方をまとめる。
公開: 更新:
express@4 を入れたプロジェクトと、同じ app.js のまま express@5 に上げたプロジェクトを並べ、同じコマンドを打ち比べた。
各節の出力は「v4 では」「v5 に上げると」「v5 で直すと」の 3 段で並ぶ。
サーバーは node app.js > server.log 2>&1 & でバックグラウンドに起動し、curl で叩いてから server.log を見た。curl -sf の -f は、400 以上の応答で本文を出さずに終了コード 22 で終わる。
サーバーに繋がらないときは 7 で終わる。
9 つのうち、起動の時点で止まるのはルートのパスの書き方の 2 つである。req.param()・req.body・req.query の 3 つは起動はでき、そのコードを通るリクエストだけがエラーの応答になる(curl は 400 以上の応答を示す終了コード 22 で終わる)。
残りの 4 つは例外を出さずに、応答の中身か起動の結果が変わる。
4.22.3 で動かすと、5 で消えたメソッドと引数の形には server.log に express deprecated の警告が出る。
ここで打ったものでは req.param()・res.json(obj, status)・res.send(status)・res.redirect("back") が当てはまり、extended を省いた express.urlencoded() には body-parser deprecated が出る。
上げる前に 4 のまま一通りリクエストを流してログを見れば、こうした書き方は見つかる。
パスの * と ? は、4 の server.log に警告が出なかった。
公式の移行ガイドには、書き換えをまとめて行う codemod(npx codemod@latest @expressjs/v5-migration-recipe)も載っている。
5 は Node.js 18 以上で動く。
このページで打っていない変更点は、公式の移行ガイドに一覧がある。res.status() が 100〜999 の整数しか受け付けない、res.vary() の引数が必須になった、res.clearCookie() が maxAge と expires を無視する、といった変更である。req.host がポート番号を含むようになり、router.param() は名前の配列を受け付けなくなった。res.sendFile() に渡す絶対パスにドットで始まるディレクトリが含まれると、404 になる。
req.params の中身も変わった(移行ガイドによる)。
名前を付けたワイルドカードの値は配列で入り、一致しなかったパラメータはキーごと無くなる。
文字列で書いたパスでは、req.params はプロトタイプの無いオブジェクトになり、req.params.hasOwnProperty() は呼べない。
4 の * は一致した部分を req.params[0] に文字列で入れていたので、そこを読むハンドラは req.params.splat.join("/") のように書き換える。
このページでは req.params の値は打っていない。
/:id(\d+) のようにパラメータの後ろに書いた正規表現や、/:path+・/:path* のような繰り返しの記号も、5 が使う path-to-regexp 8 では起動の時点で例外になる(path-to-regexp の README による。
このページでは打っていない)。
値の形はハンドラーの中で確かめ、残りのパスは {/*path} で受ける。
名前の無い * のルートで起動できなくなった
Express 5 はルートのパスの解析に path-to-regexp 8 を使う(4.22.3 は 0.1.13)。
8 では * が名前の付いたワイルドカードの記号になり、名前の無い * は登録の時点で例外になる。
SPA の画面を app.get("*") で返すコードや、API の 404 を app.use("/api/*") で返すコードが当てはまる。
v4 では
$ cat app.js
const express = require("express");
const app = express();
app.get("/api/users", (req, res) => res.json([{ id: 1, name: "sato" }]));
// API 以外のパスには SPA の画面を返す
app.get("*", (req, res) => res.send("<h1>SPA</h1>"));
app.listen(3000, () => console.log("listening on 3000"));$ node app.js > server.log 2>&1 &$ curl -s http://localhost:3000/about
<h1>SPA</h1>
$ echo $?
0$ cat server.log
listening on 3000v5 に上げると
$ node app.js > server.log 2>&1 &$ curl -s http://localhost:3000/about
$ echo $?
7$ cat server.log
/work/v5/node_modules/path-to-regexp/dist/index.js:108
throw new PathError(`Missing parameter name at index ${index}`, str);
^
PathError [TypeError]: Missing parameter name at index 1: *; visit https://git.new/pathToRegexpError for info
at consumeUntil (/work/v5/node_modules/path-to-regexp/dist/index.js:108:27)
at parse (/work/v5/node_modules/path-to-regexp/dist/index.js:140:26)
at process (/work/v5/node_modules/path-to-regexp/dist/index.js:263:56)
at pathToRegexp (/work/v5/node_modules/path-to-regexp/dist/index.js:274:5)
at Object.match (/work/v5/node_modules/path-to-regexp/dist/index.js:225:30)
at matcher (/work/v5/node_modules/router/lib/layer.js:86:23)
at new Layer (/work/v5/node_modules/router/lib/layer.js:93:62)
at router.route (/work/v5/node_modules/router/index.js:428:17)
at app.route (/work/v5/node_modules/express/lib/application.js:257:22)
at app.<computed> [as get] (/work/v5/node_modules/express/lib/application.js:478:22) {
originalPath: '*'
}
Node.js v24.22.2v5 で直すと
$ cat app.js
const express = require("express");
const app = express();
app.get("/api/users", (req, res) => res.json([{ id: 1, name: "sato" }]));
// API 以外のパスには SPA の画面を返す
app.get("/{*splat}", (req, res) => res.send("<h1>SPA</h1>"));
app.listen(3000, () => console.log("listening on 3000"));$ node app.js > server.log 2>&1 &$ curl -s http://localhost:3000/about
<h1>SPA</h1>
$ echo $?
0$ curl -s http://localhost:3000/
<h1>SPA</h1>
$ echo $?
0ワイルドカードに名前を付け、/{*splat} と書く。splat は好きな名前でよく、一致した部分は req.params.splat に配列で入る。
波括弧を付けない /*splat は / そのものには一致しないので、トップページも同じハンドラで返すなら波括弧で囲む。
v4 の server.log には警告が何も出ていない。
パスの書き方は 4 のログからは見つけられないので、app.get や app.use に渡すパスの * を検索して書き換える。
省略できるパラメータの ? で起動できなくなった
4 では /users/:id? と書くと、/users と /users/7 の両方に一致した。
5 では ? を書けなくなり、Unexpected ? の例外で起動できない。
公式の移行ガイドによると、/[discussion|page]/:slug のようにパスへ正規表現の文字を書く書き方も 5 では使えない。
こちらはこのページでは打っていない。
v4 では
$ cat app.js
const express = require("express");
const app = express();
// /users で一覧、/users/7 で 1 人を返す
app.get("/users/:id?", (req, res) => {
res.json(req.params.id ? { id: req.params.id } : [{ id: 1 }, { id: 2 }]);
});
app.listen(3000, () => console.log("listening on 3000"));$ node app.js > server.log 2>&1 &$ curl -s http://localhost:3000/users
[{"id":1},{"id":2}]
$ echo $?
0$ curl -s http://localhost:3000/users/7
{"id":"7"}
$ echo $?
0$ cat server.log
listening on 3000v5 に上げると
$ node app.js > server.log 2>&1 &$ curl -s http://localhost:3000/users
$ echo $?
7$ cat server.log
/work/v5/node_modules/path-to-regexp/dist/index.js:130
throw new PathError(`Unexpected ${value} at index ${index - 1}`, str);
^
PathError [TypeError]: Unexpected ? at index 10: /users/:id?; visit https://git.new/pathToRegexpError for info
at consumeUntil (/work/v5/node_modules/path-to-regexp/dist/index.js:130:23)
at parse (/work/v5/node_modules/path-to-regexp/dist/index.js:140:26)
at process (/work/v5/node_modules/path-to-regexp/dist/index.js:263:56)
at pathToRegexp (/work/v5/node_modules/path-to-regexp/dist/index.js:274:5)
at Object.match (/work/v5/node_modules/path-to-regexp/dist/index.js:225:30)
at matcher (/work/v5/node_modules/router/lib/layer.js:86:23)
at new Layer (/work/v5/node_modules/router/lib/layer.js:93:62)
at router.route (/work/v5/node_modules/router/index.js:428:17)
at app.route (/work/v5/node_modules/express/lib/application.js:257:22)
at app.<computed> [as get] (/work/v5/node_modules/express/lib/application.js:478:22) {
originalPath: '/users/:id?'
}
Node.js v24.22.2v5 で直すと
$ cat app.js
const express = require("express");
const app = express();
// /users で一覧、/users/7 で 1 人を返す
app.get("/users{/:id}", (req, res) => {
res.json(req.params.id ? { id: req.params.id } : [{ id: 1 }, { id: 2 }]);
});
app.listen(3000, () => console.log("listening on 3000"));$ node app.js > server.log 2>&1 &$ curl -s http://localhost:3000/users
[{"id":1},{"id":2}]
$ echo $?
0$ curl -s http://localhost:3000/users/7
{"id":"7"}
$ echo $?
0省略できる部分を波括弧で囲み、/users{/:id} と書く。
波括弧の中の / も一緒に省略されるので、/users と /users/7 の両方に一致する。
正規表現の文字を使っていたパスは、公式の移行ガイドの例のとおり、["/discussion/:slug", "/page/:slug"] のように候補ごとのパスを配列で並べる。
req.param() が無くなった
req.param(name) は、パスのパラメータ・ボディ・クエリの順に名前を探して値を返すメソッドだった。
4 では非推奨で、server.log へ警告が出る。
5 では消えたので、起動はできても、呼んだリクエストが TypeError でエラーの応答になる。
v4 では
$ cat app.js
const express = require("express");
const app = express();
app.get("/users/:id", (req, res) => {
res.json({ id: req.param("id") });
});
app.listen(3000, () => console.log("listening on 3000"));$ node app.js > server.log 2>&1 &$ curl -sf http://localhost:3000/users/7
{"id":"7"}
$ echo $?
0$ cat server.log
listening on 3000
Sun, 08 Nov 2026 03:33:36 GMT express deprecated req.param(name): Use req.params, req.body, or req.query instead at app.js:5:22v5 に上げると
$ node app.js > server.log 2>&1 &$ curl -sf http://localhost:3000/users/7
$ echo $?
22$ cat server.log
listening on 3000
TypeError: req.param is not a function
at /work/v5/req-param/app.js:5:22
at Layer.handleRequest (/work/v5/node_modules/router/lib/layer.js:152:17)
at next (/work/v5/node_modules/router/lib/route.js:157:13)
at Route.dispatch (/work/v5/node_modules/router/lib/route.js:117:3)
at handle (/work/v5/node_modules/router/index.js:435:11)
at Layer.handleRequest (/work/v5/node_modules/router/lib/layer.js:152:17)
at /work/v5/node_modules/router/index.js:295:15
at param (/work/v5/node_modules/router/index.js:600:14)
at param (/work/v5/node_modules/router/index.js:610:14)
at processParams (/work/v5/node_modules/router/index.js:664:3)v5 で直すと
$ cat app.js
const express = require("express");
const app = express();
app.get("/users/:id", (req, res) => {
res.json({ id: req.params.id });
});
app.listen(3000, () => console.log("listening on 3000"));$ node app.js > server.log 2>&1 &$ curl -sf http://localhost:3000/users/7
{"id":"7"}
$ echo $?
0値の出どころに合わせて、req.params・req.body・req.query のどれかから読む。
公式の移行ガイドによると、4 で新しい名前に置き換わった app.del()・res.sendfile()・req.acceptsLanguage() も 5 では消えた。
新しい名前はそれぞれ app.delete()・res.sendFile()・req.acceptsLanguages() である。
この 3 つはこのページでは打っていない。
res.json(obj, 201) や res.redirect('back') が別の応答を返すようになった
4 では res.json() と res.send() の 2 つ目の引数にステータスコードを渡せた。res.send(404) のように数値だけを渡すと、その数値をステータスコードとして返した。res.redirect("back") は、リクエストの Referer へ戻す特別な書き方だった。
どれも 4 では非推奨で、5 では消えた。
ここで打った 3 つの書き方は、消えたあともエラーにはならなかった。res.send(body, status) の形は打っていない。
ここでは curl の -w で、本文の後ろにステータスコードを出した。
v4 では
$ cat app.js
const express = require("express");
const app = express();
app.post("/users", (req, res) => res.json({ id: 1 }, 201));
app.get("/users/99", (req, res) => res.send(404));
app.post("/login", (req, res) => res.redirect("back"));
app.listen(3000, () => console.log("listening on 3000"));$ node app.js > server.log 2>&1 &$ curl -s -w ' %{http_code}\n' -X POST http://localhost:3000/users
{"id":1} 201$ curl -s -w ' %{http_code}\n' http://localhost:3000/users/99
Not Found 404$ curl -s -w ' %{http_code}\n' -X POST -H "Referer: http://localhost:3000/form" http://localhost:3000/login
Found. Redirecting to http://localhost:3000/form 302$ cat server.log
listening on 3000
Sun, 08 Nov 2026 03:33:39 GMT express deprecated res.json(obj, status): Use res.status(status).json(obj) instead at app.js:4:38
Sun, 08 Nov 2026 03:33:39 GMT express deprecated res.send(status): Use res.sendStatus(status) instead at app.js:5:40
Sun, 08 Nov 2026 03:33:39 GMT express deprecated res.location("back"): use res.location(req.get("Referrer") || "/") and refer to https://dub.sh/security-redirect for best practices at app.js:6:38v5 に上げると
$ node app.js > server.log 2>&1 &$ curl -s -w ' %{http_code}\n' -X POST http://localhost:3000/users
{"id":1} 200$ curl -s -w ' %{http_code}\n' http://localhost:3000/users/99
404 200$ curl -s -w ' %{http_code}\n' -X POST -H "Referer: http://localhost:3000/form" http://localhost:3000/login
Found. Redirecting to back 302$ cat server.log
listening on 3000v5 で直すと
$ cat app.js
const express = require("express");
const app = express();
app.post("/users", (req, res) => res.status(201).json({ id: 1 }));
app.get("/users/99", (req, res) => res.sendStatus(404));
app.post("/login", (req, res) => res.redirect(req.get("Referrer") || "/"));
app.listen(3000, () => console.log("listening on 3000"));$ node app.js > server.log 2>&1 &$ curl -s -w ' %{http_code}\n' -X POST http://localhost:3000/users
{"id":1} 201$ curl -s -w ' %{http_code}\n' http://localhost:3000/users/99
Not Found 404$ curl -s -w ' %{http_code}\n' -X POST -H "Referer: http://localhost:3000/form" http://localhost:3000/login
Found. Redirecting to http://localhost:3000/form 3025 では res.json({ id: 1 }, 201) の 201 が無視されて 200 になり、res.send(404) は本文が 404 のまま 200 で返った。res.redirect("back") は、出力の Redirecting to back のとおり back という相対パスへのリダイレクトになった。
相対パスなので、ブラウザは /login の隣の /back へ移る。
5 の server.log には警告も出ないので、テストでステータスコードを確かめていないと気付かない。
公式の移行ガイドは、消えたメソッドを使うとアプリが落ちると書いている。
この 3 つは例外を出さず、応答だけが変わった。
ステータスコードは res.status(201).json(...) のように先に決め、本文の要らない応答は res.sendStatus(404) で返す。
前のページへ戻すときは、req.get("Referrer") || "/" を自分で渡す。res.redirect(url, 301) の順で書いていたコードは、5 では TypeError: Invalid status code で落ちるので、res.redirect(301, url) に入れ替える。
このページでは打っていない。res.status() に整数以外を渡したときに出る同じエラーは、Invalid status code の記事 で打っている。
パーサーが読まなかった req.body が undefined になった
express.json() は、Content-Type が application/json のリクエストだけを読む。
4 では、読まなかったリクエストでも req.body に空のオブジェクト {} が入っていた。
5 では undefined のままになり、req.body を分割代入しているハンドラが TypeError で落ちる。
フォームの送信(application/x-www-form-urlencoded)が当てはまる。
ボディの無い POST も同じはずだが、このページでは打っていない。
v4 では
$ cat app.js
const express = require("express");
const app = express();
app.use(express.json());
app.post("/users", (req, res) => {
const { name } = req.body;
res.json({ name: name ?? null });
});
app.listen(3000, () => console.log("listening on 3000"));$ node app.js > server.log 2>&1 &$ curl -sf -X POST -H "Content-Type: application/json" -d '{"name":"sato"}' http://localhost:3000/users
{"name":"sato"}
$ echo $?
0$ curl -sf -X POST -d name=sato http://localhost:3000/users
{"name":null}
$ echo $?
0v5 に上げると
$ node app.js > server.log 2>&1 &$ curl -sf -X POST -H "Content-Type: application/json" -d '{"name":"sato"}' http://localhost:3000/users
{"name":"sato"}
$ echo $?
0$ curl -sf -X POST -d name=sato http://localhost:3000/users
$ echo $?
22$ cat server.log
listening on 3000
TypeError: Cannot destructure property 'name' of 'req.body' as it is undefined.
at /work/v5/req-body/app.js:7:11
at Layer.handleRequest (/work/v5/node_modules/router/lib/layer.js:152:17)
at next (/work/v5/node_modules/router/lib/route.js:157:13)
at Route.dispatch (/work/v5/node_modules/router/lib/route.js:117:3)
at handle (/work/v5/node_modules/router/index.js:435:11)
at Layer.handleRequest (/work/v5/node_modules/router/lib/layer.js:152:17)
at /work/v5/node_modules/router/index.js:295:15
at processParams (/work/v5/node_modules/router/index.js:582:12)
at next (/work/v5/node_modules/router/index.js:291:5)
at read (/work/v5/node_modules/body-parser/lib/read.js:68:5)v5 で直すと
$ cat app.js
const express = require("express");
const app = express();
app.use(express.json());
app.post("/users", (req, res) => {
const { name } = req.body ?? {};
res.json({ name: name ?? null });
});
app.listen(3000, () => console.log("listening on 3000"));$ node app.js > server.log 2>&1 &$ curl -sf -X POST -H "Content-Type: application/json" -d '{"name":"sato"}' http://localhost:3000/users
{"name":"sato"}
$ echo $?
0$ curl -sf -X POST -d name=sato http://localhost:3000/users
{"name":null}
$ echo $?
0req.body ?? {} のように、undefined のときの値を決めてから読む。
フォームも受け付けるなら app.use(express.urlencoded()) を足すと、name=sato が req.body.name に入る。
ボディの無いリクエストはどのパーサーも読まないので、?? {} は残しておく。
角括弧で書いたクエリとフォームの値が入れ子にならなくなった
4 のクエリのパーサーは qs を使う extended で、filter[status]=open を { filter: { status: "open" } } の入れ子に読んだ。
5 の既定は Node.js の querystring を使う simple で、角括弧はキーの名前の一部として残る。express.urlencoded() の extended の既定も true から false に変わり、フォームの値も入れ子にならない。
curl の -g は、URL の [] を curl 自身の展開に使わせないための指定である。
v4 では
$ cat app.js
const express = require("express");
const app = express();
app.use(express.urlencoded());
app.get("/search", (req, res) => res.json(req.query));
app.post("/profile", (req, res) => res.json(req.body));
app.listen(3000, () => console.log("listening on 3000"));$ node app.js > server.log 2>&1 &$ curl -s -g "http://localhost:3000/search?filter[status]=open&tags[]=a&tags[]=b"
{"filter":{"status":"open"},"tags":["a","b"]}$ curl -s -g 'http://localhost:3000/search?name[$ne]=x&$where=1'
{"name":{"$ne":"x"},"$where":"1"}$ curl -s -d "user[name]=sato&user[age]=20" http://localhost:3000/profile
{"user":{"name":"sato","age":"20"}}$ cat server.log
Sun, 08 Nov 2026 03:33:43 GMT body-parser deprecated undefined extended: provide extended option at app.js:4:17
listening on 3000v5 に上げると
$ node app.js > server.log 2>&1 &$ curl -s -g "http://localhost:3000/search?filter[status]=open&tags[]=a&tags[]=b"
{"filter[status]":"open","tags[]":["a","b"]}$ curl -s -g 'http://localhost:3000/search?name[$ne]=x&$where=1'
{"name[$ne]":"x","$where":"1"}$ curl -s -d "user[name]=sato&user[age]=20" http://localhost:3000/profile
{"user[name]":"sato","user[age]":"20"}v5 で直すと
$ cat app.js
const express = require("express");
const app = express();
app.set("query parser", "extended");
app.use(express.urlencoded({ extended: true }));
app.get("/search", (req, res) => res.json(req.query));
app.post("/profile", (req, res) => res.json(req.body));
app.listen(3000, () => console.log("listening on 3000"));$ node app.js > server.log 2>&1 &$ curl -s -g "http://localhost:3000/search?filter[status]=open&tags[]=a&tags[]=b"
{"filter":{"status":"open"},"tags":["a","b"]}$ curl -s -g 'http://localhost:3000/search?name[$ne]=x&$where=1'
{"name":{"$ne":"x"},"$where":"1"}$ curl -s -d "user[name]=sato&user[age]=20" http://localhost:3000/profile
{"user":{"name":"sato","age":"20"}}入れ子のまま読みたいなら、app.set("query parser", "extended") でクエリのパーサーを 4 と同じ qs に戻し、express.urlencoded({ extended: true }) と書く。tags[] のように同じ名前を並べた値は 5 の既定でも配列になり、キーの名前に [] が残るだけである。
name[$ne]=x&$where=1 の行は、MongoDB の演算子を混ぜたクエリである。extended のパーサーは name を { "$ne": "x" } のオブジェクトにし、5 の既定の simple は name[$ne] という名前の文字列のまま渡した。
キーそのものが $ で始まる $where は、どちらのパーサーでもそのまま届いた。
こうした値を MongoDB の検索条件にそのまま渡すと、演算子として読まれる。
MongoDB に渡すところは、このページでは打っていない。
req.query に代入するミドルウェアが落ちるようになった
5 の req.query は、読み取り専用のプロパティ(getter)になった。req.query そのものに代入すると、strict モードのコードでは TypeError になる。
公式の移行ガイドは、req.query.page = 1 のようにプロパティを書き換えることも 5 ではできないと書いている。
express-mongo-sanitize は、req.body や req.query から $ で始まるキーと . を含むキーを取り除いて代入し直すミドルウェアである。
2026-11-07 時点で最新の 2.2.0 を 5 で使うと、クエリの無い GET /users までエラーの応答になった(curl の終了コード 22)。
v4 では
$ cat app.js
const express = require("express");
const mongoSanitize = require("express-mongo-sanitize");
const app = express();
app.use(express.json());
// req.body・req.params・req.query などから $ で始まるキーを取り除く
app.use(mongoSanitize());
app.get("/users", (req, res) => res.json(req.query));
app.post("/users", (req, res) => res.json(req.body));
app.listen(3000, () => console.log("listening on 3000"));$ node app.js > server.log 2>&1 &$ curl -sf -g 'http://localhost:3000/users?name[$ne]=x&$where=1'
{"name":{}}
$ echo $?
0$ curl -sf -X POST -H "Content-Type: application/json" -d '{"name":{"$ne":"x"}}' http://localhost:3000/users
{"name":{}}
$ echo $?
0v5 に上げると
$ node app.js > server.log 2>&1 &$ curl -sf -g 'http://localhost:3000/users?name[$ne]=x&$where=1'
$ echo $?
22$ cat server.log
listening on 3000
TypeError: Cannot set property query of #<IncomingMessage> which has only a getter
at /work/v5/node_modules/express-mongo-sanitize/index.js:113:18
at Array.forEach (<anonymous>)
at /work/v5/node_modules/express-mongo-sanitize/index.js:110:44
at Layer.handleRequest (/work/v5/node_modules/router/lib/layer.js:152:17)
at trimPrefix (/work/v5/node_modules/router/index.js:342:13)
at /work/v5/node_modules/router/index.js:297:9
at processParams (/work/v5/node_modules/router/index.js:582:12)
at next (/work/v5/node_modules/router/index.js:291:5)
at read (/work/v5/node_modules/body-parser/lib/read.js:59:5)
at jsonParser (/work/v5/node_modules/body-parser/lib/types/json.js:60:5)$ curl -sf http://localhost:3000/users
$ echo $?
22$ curl -sf -X POST -H "Content-Type: application/json" -d '{"name":{"$ne":"x"}}' http://localhost:3000/users
$ echo $?
22v5 で直すと
$ cat app.js
const express = require("express");
const mongoSanitize = require("express-mongo-sanitize");
const app = express();
app.use(express.json());
// req.query は書き換えられないので、req.body だけをその場で書き換える
app.use((req, res, next) => {
mongoSanitize.sanitize(req.body);
next();
});
app.get("/users", (req, res) => {
// req.query は読むたびに作り直されるので、取り除いた結果を変数に受けて使う
const query = mongoSanitize.sanitize(req.query);
res.json(query);
});
app.post("/users", (req, res) => res.json(req.body));
app.listen(3000, () => console.log("listening on 3000"));$ node app.js > server.log 2>&1 &$ curl -sf -g 'http://localhost:3000/users?name[$ne]=x&$where=1'
{"name[$ne]":"x"}
$ echo $?
0$ curl -sf -X POST -H "Content-Type: application/json" -d '{"name":{"$ne":"x"}}' http://localhost:3000/users
{"name":{}}
$ echo $?
0req.body は 5 でも中身を書き換えられるので、ミドルウェアの中で mongoSanitize.sanitize(req.body) を呼んでその場で取り除く。req.query は、読む側で mongoSanitize.sanitize(req.query) の戻り値を使う。
直したコードでは $where が取り除かれ、入れ子にならなかった name[$ne] だけが残った。
この書き方では、mongoSanitize.sanitize(req.query) を呼ぶのは req.query を読むハンドラごとになる。
呼び忘れたハンドラには、$ で始まるキーがそのまま届く。
元のミドルウェアがしていた req.headers と req.params の処理も、この直し方では行わない。
5 の既定の simple のパーサーでも、前の節のとおり $where のように $ で始まるキーは届く。req.query を検索条件に渡すコードがあるなら、取り除く処理は 5 でも外さない。
使用中のポートで起動しても listening と出てエラーにならなくなった
4 の app.listen() は、ポートが使われていると EADDRINUSE の例外でプロセスを止めた。
5 はエラーをコールバックの 1 つ目の引数に渡す。
引数を受け取らないコールバックはエラーを捨て、listening on 3000 と出してしまう。
この app.js にはタイマーやデータベースの接続のようなほかの処理が無いので、待ち受けないままプロセスが終わり、終了コードも 0 になった。
ほかの処理が動いているアプリでは、待ち受けないままプロセスが動き続けることになる(このページでは打っていない)。
ここでは 1 つ目のサーバーをバックグラウンドで起動したまま、同じ app.js をもう一度起動した。
v4 では
$ cat app.js
const express = require("express");
const app = express();
app.get("/", (req, res) => res.send("hello"));
app.listen(3000, () => console.log("listening on 3000"));$ node app.js > server.log 2>&1 &$ node app.js
node:events:505
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (node:net:2390:16)
at listenInCluster (node:net:2499:12)
at Server.listen (node:net:2624:7)
at app.listen (/work/v4/node_modules/express/lib/application.js:635:24)
at Object.<anonymous> (/work/v4/listen-error/app.js:6:5)
at Module._compile (node:internal/modules/cjs/loader:1929:14)
at Object..js (node:internal/modules/cjs/loader:2060:10)
at Module.load (node:internal/modules/cjs/loader:1651:32)
at Module._load (node:internal/modules/cjs/loader:1443:12)
at wrapModuleLoad (node:internal/modules/cjs/loader:261:19)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:2478:8)
at process.processTicksAndRejections (node:internal/process/task_queues:90:21) {
code: 'EADDRINUSE',
errno: -98,
syscall: 'listen',
address: '::',
port: 3000
}
Node.js v24.22.2
$ echo $?
1v5 に上げると
$ node app.js > server.log 2>&1 &$ node app.js
listening on 3000
$ echo $?
0v5 で直すと
$ cat app.js
const express = require("express");
const app = express();
app.get("/", (req, res) => res.send("hello"));
app.listen(3000, (err) => {
if (err) throw err;
console.log("listening on 3000");
});$ node app.js > server.log 2>&1 &$ node app.js
/work/v5/listen-error/app.js:7
if (err) throw err;
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (node:net:2390:16)
at listenInCluster (node:net:2499:12)
at Server.listen (node:net:2624:7)
at app.listen (/work/v5/node_modules/express/lib/application.js:605:24)
at Object.<anonymous> (/work/v5/listen-error/app.js:6:5)
at Module._compile (node:internal/modules/cjs/loader:1929:14)
at Object..js (node:internal/modules/cjs/loader:2060:10)
at Module.load (node:internal/modules/cjs/loader:1651:32)
at Module._load (node:internal/modules/cjs/loader:1443:12)
at wrapModuleLoad (node:internal/modules/cjs/loader:261:19) {
code: 'EADDRINUSE',
errno: -98,
syscall: 'listen',
address: '::',
port: 3000
}
Node.js v24.22.2
$ echo $?
1コールバックで err を受け取り、あれば投げる。
4 と同じく EADDRINUSE で止まり、終了コードは 1 になる。
express.static がドットで始まるディレクトリの中を返さなくなった
express.static の dotfiles は、名前がドットで始まるファイルやディレクトリを返すかどうかを決めるオプションである。
公式の移行ガイドによると、5 で既定が "ignore" になり、.well-known のようにドットで始まるディレクトリの中のファイルは 404 になる。
4 の express.static が使う send 0.19 の README によると、4 の既定でも、名前がドットで始まるファイルそのものは返さず、ドットで始まるディレクトリの中のファイルは返していた。
移行ガイドの変更点の節は「4 ではドットファイルを既定で返していた」と書いており、README と食い違う。
ドットで始まるファイルそのものを 4 で開く形は、このページでは打っていない。
ガイドは、Android の App Links や Apple の Universal Links のように .well-known に置いたファイルを読ませる仕組みが動かなくなると書いている。
v4 では
$ find public -type f
public/.well-known/assetlinks.json$ cat app.js
const express = require("express");
const app = express();
app.use(express.static("public"));
app.listen(3000, () => console.log("listening on 3000"));$ node app.js > server.log 2>&1 &$ curl -s -w ' %{http_code}\n' http://localhost:3000/.well-known/assetlinks.json
[{"relation":["delegate_permission/common.handle_all_urls"],"target":{"namespace":"android_app","package_name":"com.example.app"}}] 200v5 に上げると
$ node app.js > server.log 2>&1 &$ curl -s -w ' %{http_code}\n' http://localhost:3000/.well-known/assetlinks.json
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /.well-known/assetlinks.json</pre>
</body>
</html>
404v5 で直すと
$ cat app.js
const express = require("express");
const app = express();
// .well-known だけを別に配る。ほかのドットで始まるファイルは返さないまま
app.use("/.well-known", express.static("public/.well-known", { dotfiles: "allow" }));
app.use(express.static("public"));
app.listen(3000, () => console.log("listening on 3000"));$ node app.js > server.log 2>&1 &$ curl -s -w ' %{http_code}\n' http://localhost:3000/.well-known/assetlinks.json
[{"relation":["delegate_permission/common.handle_all_urls"],"target":{"namespace":"android_app","package_name":"com.example.app"}}] 2004 では public/.well-known/assetlinks.json が 200 で返り、5 では同じ URL が Cannot GET の 404 になった。
サーバーは起動できるので、.well-known の URL を開くまで気付かない。
.well-known だけを別に app.use("/.well-known", ...) で配り、そこに dotfiles: "allow" を付ける。
公式の移行ガイドと同じ書き方で、ガイドによると、ほかのドットで始まるファイルは 5 の既定のまま返さない。
検証環境Node.js 24.22.2 / npm 11.19.1 / Python 3.11.2 / 2026-11-08 検証(ほか 9 件)
- 検証日
- 実行環境
node:24Debian GNU/Linux 12 (bookworm)- バージョン
- Node.js 24.22.2
- npm 11.19.1
- Python 3.11.2
- Git 2.39.5
- Express(旧) 4.22.3
- path-to-regexp(旧) 0.1.13
- body-parser(旧) 1.20.9
- Express(新) 5.3.0
- path-to-regexp(新) 8.4.2
- body-parser(新) 2.4.0
- express-mongo-sanitize 2.2.0
- curl 7.88.1
この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。