After upgrading to Express 5, handlers calling res.send(404), res.json(user, 201) and res.redirect('/login', 301) misbehave; what does each now do, and what replaces it?
answer
- status first, then the body
- a number is now a body
- extra arguments are ignored
- redirect takes status before url
basics
~20 sIn Express 5 res.send(404) answers 200 with the JSON body 404, res.json(user, 201) answers 200 because the status argument is ignored, and res.redirect('/login', 301) throws a TypeError, giving a 500. Use res.sendStatus, res.status(code).json and res.redirect(status, url).
solid answer
~40 sExpress 5 removed the 4.x signatures that mixed a status into a body call, and they fail in different ways. `res.send(404)` is no longer read as a status: a number is a body, so the client gets 200 with `404` as JSON. `res.json(user, 201)` ignores its second argument and sends 200. `res.redirect('/login', 301)` takes two arguments as `(status, url)`, so it emits two deprecation warnings and then `res.status('/login')` throws `TypeError: Invalid status code`, which the error handler turns into a 500. The replacements are `res.sendStatus(404)` or `res.status(404).send(...)`, `res.status(201).json(user)` and `res.redirect(301, '/login')`. The Express team's codemods rewrite these call sites automatically, but only tests that assert status codes prove them fixed.
code
javascript · 15 linesconst express = require('express');
const app = express();
const user = { id: 7, name: 'Ada' };
// Express 4 forms left in an upgraded codebase
app.get('/legacy/missing', (req, res) => res.send(404)); // 200, body 404
app.post('/legacy/users', (req, res) => res.json(user, 201)); // 200, 201 ignored
app.get('/legacy/account', (req, res) => res.redirect('/login', 301)); // TypeError -> 500
// Express 5 forms
app.get('/missing', (req, res) => res.sendStatus(404)); // 404 Not Found
app.post('/users', (req, res) => res.status(201).json(user)); // 201
app.get('/account', (req, res) => res.redirect(301, '/login')); // 301 to /login
app.listen(3000);go deeper
Recall the Express 5 order: res.status(code) first, then res.json or res.send; and res.redirect(status, url) with the status before the URL.
Explain what each removed form now does: a number sent as a JSON body, an ignored status argument, and a redirect that throws.
Plan the upgrade sweep: codemods, searches for two-argument calls, status assertions in route tests, and deprecation lines in the logs after deploy.
Weigh relying on codemods against requiring status-code tests on every route before the upgrade ships, and who owns the residual risk.
## Why these break quietly Express 4 accepted several overloaded forms where a status code rode along inside a body or redirect call. Express 5 removed them, as the official migration guide lists, and the response methods now take one job each: `res.status()` sets the code, the body methods send the body. The danger is that two of the three old forms **do not crash**. They produce a successful-looking response with the wrong status, which slips past any check that only looks for errors. ## What each old call does in Express 5 | Express 4 form | What Express 5 does with it | Express 5 replacement | |---|---|---| | `res.send(404)` | treats the number as a body: 200, `Content-Type: application/json`, body `404` | `res.sendStatus(404)` or `res.status(404).send('...')` | | `res.send(body, 200)` | ignores the extra argument | `res.status(200).send(body)` | | `res.json(user, 201)` | ignores the second argument: 200 | `res.status(201).json(user)` | | `res.jsonp(obj, status)` | ignores the second argument | `res.status(status).jsonp(obj)` | | `res.redirect('/login', 301)` | reads it as `(status, url)`: deprecation warnings, then a `TypeError` and a 500 | `res.redirect(301, '/login')` | | `res.redirect('back')` | the magic string is gone: `Location: back`, a relative path | `res.redirect(req.get('Referrer') \|\| '/')` | Two related changes surface in the same sweep: - **`res.status()` validates its argument.** It throws a `TypeError` unless the code is an integer and a `RangeError` outside 100 to 999. A status read from configuration as the string `'201'` now crashes the handler instead of being passed through. - **`res.sendfile()` is gone**; use `res.sendFile()`. ## How `res.send` reads its argument now `res.send` chooses what to do from the type of its one argument, which is why a number no longer means a status: - a **string** is sent as is, with an HTML `Content-Type` unless one was already set; - a **Buffer** or other byte view is sent as binary, as `application/octet-stream` unless a type was set; - a **number, boolean, object or array** is handed to `res.json`, so `404` becomes the JSON text `404`; - **`null`** sends an empty body. ## Tracing the redirect failure `res.redirect` decides its signature by argument count. With two arguments it assumes the first is the status. So `res.redirect('/login', 301)`: 1. warns `Url must be a string` (the URL slot holds 301); 2. warns `Status must be a number` (the status slot holds `'/login'`); 3. sets a `Location` header from 301; 4. calls `res.status('/login')`, which throws `TypeError: Invalid status code: "/login". Status code must be an integer.` Express catches the synchronous throw and forwards it to error-handling middleware, so the user sees a 500 page instead of a redirect. The deprecation lines in the log are the clue. ## Why the silent ones hurt most A crash shows up in error-rate dashboards on the first request. A wrong status does not: - **Monitoring** counts `res.send(404)` as a success, so a broken lookup route looks healthy. - **Clients** that branch on status treat a missing record as found and try to read the body `404` as data. - **Caches** in front of the service may store a 200 that should have been a 404, and keep serving it. - **Creation endpoints** that lost their 201 still work for most callers, so the regression can sit unnoticed until a client checks the code. The same upgrade also tightened `res.vary()`, which now throws when its field argument is missing, and `res.clearCookie()`, which ignores `maxAge` and `expires`. ## Finding and fixing them The Express team publishes codemods for the upgrade: - `npx codemod@latest @expressjs/v5-migration-recipe` runs the whole set; - individual recipes such as `@expressjs/status-send-order` and `@expressjs/redirect-arg-order` target these call sites. A codemod cannot see a status computed at runtime or a call hidden behind a helper, so back it up: - **search** for `res.send(` with a numeric literal, and for `res.json(` or `res.redirect(` with two arguments; - **assert status codes** in route tests, not just bodies: `res.send(404)` returning 200 is exactly what a body-only test misses; - **watch the logs** after deploy for `express deprecated` lines, which point at the file and line of each surviving redirect. ## The rule to teach Status first, then the body: `res.status(code)` returns the response for chaining, and `res.json`, `res.send` and `res.sendStatus` finish it. Apart from `res.status` and `res.sendStatus`, whose whole job is the status, `res.redirect` is the one response method here that takes a status, and there it comes **before** the URL.
- How do you send a number as a plain-text body in Express 5 without it being sent as JSON?Convert it to a string first, as the migration guide suggests: `res.send(String(count))` sends text with an HTML content type unless you set one, or use `res.type('text').send(String(count))`. Passing the bare number makes `res.send` hand it to `res.json`, so the body is JSON with status 200.
- A config file supplies the success status as the string '201'. What happens in Express 5 when a handler calls res.status(config.status)?Express 5's `res.status()` throws a `TypeError` because the code is not an integer, and the error handler answers 500. Convert it with `Number()` when loading configuration and reject anything outside 100 to 999, which `res.status()` would otherwise reject with a `RangeError`.
- res.send(404) still sets a 404 status in Express 5
- res.json(user, 201) sends a 201 Created
- res.redirect(url, status) works with either argument order
- res.redirect('back') still goes to the referring page
- The codemod alone proves every call site is fixed
- res.status() accepts a numeric string like '201'
Assumes Express 5, which removed res.send(status), res.json(obj, status) and res.redirect(url, status); Express 4 accepted them.