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) の順に書けば直る。
公開: 更新:
curl -s --fail-with-body -w ' %{http_code}\n' -X POST http://localhost:3000/users {"error":"TypeError: Invalid status code: \"201\". Status code must be an integer."} 500
curl -s --fail-with-body -w ' %{http_code}\n' -X POST http://localhost:3000/users {"id":1} 201
2026-11-07 に node:24(Express 5.3.0 / Node.js 24.22.2) で実際に打って取った出力。検証環境の詳細
要約
Express 5 で res.status() に整数以外を渡すと、応答を返す前に次の例外が出て、リクエストは 500 になる。
TypeError: Invalid status code: "201". Status code must be an integer.5 の res.status() は 100〜999 の整数しか受け付けない(移行ガイド(新しいタブで開く)の「res.status」)。"201" が引用符付きで出ているのは、文字列を渡したからである。
4.22.3 は res.status("201") に express deprecated res.status("201") の警告を出したうえで、201 を返していた。
res.redirect("/users", 301) のように引数を逆に書いた場合も、5 は 1 つ目の "/users" をステータスコードとして読み、Invalid status code: "/users" で落ちる。
実行例
node:24 のコンテナで Express 5.3.0 に文字列の "201" と逆の順の res.redirect() を書くと、どちらも TypeError の 500 で、curl は終了コード 22 で終わった(--fail-with-body は 400 以上の応答で本文を出す)。
エラーハンドラーの err.status が undefined のときは、元のエラーの代わりに Invalid status code: undefined だけがログに残った。
$ npm ls express
tmp.l463f896vy@1.0.0 /tmp/tmp.L463F896vy
`-- express@5.3.0$ cat app.js
const express = require("express");
const app = express();
app.post("/users", (req, res) => res.status("201").json({ id: 1 }));
app.get("/old-users", (req, res) => res.redirect("/users", 301));
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: String(err) });
});
app.listen(3000, () => console.log("listening on 3000"));$ node app.js > server.log 2>&1 &$ curl -s --fail-with-body -w ' %{http_code}\n' -X POST http://localhost:3000/users
{"error":"TypeError: Invalid status code: \"201\". Status code must be an integer."} 500
$ echo $?
22$ curl -s --fail-with-body -w ' %{http_code}\n' http://localhost:3000/old-users
{"error":"TypeError: Invalid status code: \"/users\". Status code must be an integer."} 500
$ echo $?
22$ grep -v "^ at " server.log
listening on 3000
TypeError: Invalid status code: "201". Status code must be an integer.
Fri, 06 Nov 2026 21:52:41 GMT express deprecated Url must be a string at app.js:5:41
Fri, 06 Nov 2026 21:52:41 GMT express deprecated Status must be a number at app.js:5:41
TypeError: Invalid status code: "/users". Status code must be an integer.$ grep -n -e status -e redirect app.js
4:app.post("/users", (req, res) => res.status(201).json({ id: 1 }));
5:app.get("/old-users", (req, res) => res.redirect(301, "/users"));
9: res.status(500).json({ error: String(err) });$ node app.js > server.log 2>&1 &$ curl -s --fail-with-body -w ' %{http_code}\n' -X POST http://localhost:3000/users
{"id":1} 201
$ echo $?
0$ curl -s --fail-with-body -w ' %{http_code}\n' http://localhost:3000/old-users
Moved Permanently. Redirecting to /users 301
$ echo $?
0$ cat errors.js
const express = require("express");
const app = express();
app.get("/users/:id", (req, res) => {
throw new Error("database is not connected");
});
app.use((err, req, res, next) => {
res.status(err.status).json({ error: String(err) });
});
app.listen(3000, () => console.log("listening on 3000"));$ node errors.js > server.log 2>&1 &$ curl -s -o /dev/null -w '%{http_code}\n' http://localhost:3000/users/7
500$ grep -v "^ at " server.log
listening on 3000
TypeError: Invalid status code: undefined. Status code must be an integer.$ grep -n status errors.js
9: res.status(err.status ?? 500).json({ error: String(err) });$ node errors.js > server.log 2>&1 &$ curl -s -w ' %{http_code}\n' http://localhost:3000/users/7
{"error":"Error: database is not connected"} 500— 2026-11-07 時点の出力
検証環境Node.js 24.22.2 / npm 11.19.1 / Python 3.11.2 / 2026-11-07 検証(ほか 2 件)
- 検証日
- 実行環境
node:24Debian GNU/Linux 12 (bookworm)- バージョン
- Node.js 24.22.2
- npm 11.19.1
- Python 3.11.2
- Git 2.39.5
- Express 5.3.0
この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。
解決策
1. 整数で渡す
res.status(201).json({ id: 1 });
// 文字列で届く値は数値に直し、整数かを確かめてから渡す
const code = Number(process.env.CREATED_STATUS ?? 201);
res.status(Number.isInteger(code) ? code : 201).json({ id: 1 });Number("abc") は NaN になり、メッセージには Invalid status code: null と出る。res.sendStatus("404") も同じ TypeError になる。
2. res.redirect() は (status, url) の順に書く
res.redirect(301, "/users");Response の API(新しいタブで開く) では、1 つ目の引数が省略できるステータスコードで、2 つ目がリダイレクト先である。
4.22.3 は、逆の順で呼んだ箇所に express deprecated res.redirect(url, status) の警告を出すので、上げる前に 4 のログを見れば直す場所が分かる。
3. エラーハンドラーで status が無いときの値を決める
app.use((err, req, res, next) => {
res.status(err.status ?? 500).json({ error: String(err) });
});status を持たないエラーは 500 で返り、元のエラーの内容も応答に残る。
よくある原因
- ステータスコードを文字列で渡している。
環境変数やreq.queryから読んだ値は、数字だけでも文字列である。
小数の200.5も同じ TypeError になり、範囲の外の1000はRangeError: Invalid status code: 1000. Status code must be greater than 99 and less than 1000.になる。 res.redirect(url, status)の順で書いている。
4 はこの順でも警告を出して動いたが、5 では使えない。
5.3.0 のログには、TypeError の前にexpress deprecated Url must be a stringとStatus must be a numberの 2 行が出る。- エラーハンドラーで
res.status(err.status)と書いている。new Error()で作ったエラーにはstatusが無く、Invalid status code: undefinedで落ちる。
このとき、エラーハンドラーが受け取った元のエラーはログにも応答にも残らない。
4 の同じコードは、Node.js のRangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefinedで落ちていた。