Releases: expressjs/codemod
v5-migration-recipe@1.0.0
Migrate recipes for Express.js v5
This codemod migration recipe helps you update your Express.js v4 applications to be compatible with Express.js v5 by addressing deprecated APIs.
Included transformations:
- Back Redirect Deprecated: This transformation updates instances of
res.redirect('back')andres.location('back')to use the recommended alternatives. Registry entry: https://app.codemod.com/registry/@expressjs/back-redirect-deprecated. - Explicit Request Params: Migrates usage of the legacy API
req.param(name)to the current recommended alternatives. Registry entry: https://app.codemod.com/registry/@expressjs/explicit-request-params. - Pluralize Method Names: Migrates deprecated singular request methods to their pluralized counterparts where applicable. Registry entry: https://app.codemod.com/registry/@expressjs/pluralize-method-names.
- Status Send Order: Migrates usages of
res.send(status),res.send(obj, status),res.json(obj, status), andres.jsonp(obj, status)to the recommended argument ordering. Registry entry: https://app.codemod.com/registry/@expressjs/status-send-order. - Redirect Arg Order: Converts
res.redirect(url, status)calls to the recommendedres.redirect(status, url)ordering. Registry entry: https://app.codemod.com/registry/@expressjs/redirect-arg-order. - Camelcase Sendfile: Replaces legacy
res.sendfile(file)usages with the camel-casedres.sendFile(file)API. Registry entry: https://app.codemod.com/registry/@expressjs/camelcase-sendfile. - Route Del to Delete: Migrates usage of the legacy APIs
app.del()toapp.delete(). Registry entry: https://app.codemod.com/registry/@expressjs/route-del-to-delete.
References
route-del-to-delete@1.0.0
Migrate legacy app.del() to app.delete()
Migrates usage of the legacy APIs app.del() to app.delete().
Initially, del was used instead of delete, because delete is a reserved keyword in JavaScript. However, as of ECMAScript 6, delete and other reserved keywords can legally be used as property names. The app.del() method was deprecated in Express 4 and removed in Express 5.
Example
Migrating app.del()
The migration involves replacing instances of app.del() with app.delete().
- app.del('/some-route', (req, res) => {
+ app.delete('/some-route', (req, res) => {
// Some logic here
});References
redirect-arg-order@1.0.0
Migrate legacy res.redirect(url, status)
Migrates usage of the legacy APIs res.redirect(url, status) to the new signature
res.redirect(status, url). This usage was deprecated in Express 4, in Express 5 you must use the new signature res.redirect(status, url).
Example
Migrating res.redirect(url, status)
The migration involves replacing instances of res.redirect(url, status) with res.redirect(status, url).
app.get('/some-route', (req, res) => {
// Some logic here
- res.redirect(url, status);
+ res.redirect(status, url);
});References
explicit-request-params@1.0.0
Migrate legacy req.param(name)
The req.param(name) helper that used to magically look up values from multiple places has been removed. This potentially confusing and dangerous method of retrieving form data has been removed. You will now need to specifically look for the submitted parameter name in the req.params, req.body, or req.query object.
Examples
Replacing req.param('body') and req.param('query')
Replace req.param('body') with req.body and
req.param('query') with req.query.
app.get('/', (req, res) => {
// Before
- const reqBody = req.param('body');
- const reqQuery = req.param('query');
// After
+ const reqBody = req.body;
+ const reqQuery = req.query;
});Replacing req.param('paramName')
Replace req.param('paramName') with req.params.paramName.
app.get('/user/:id', (req, res) => {
// Before
- const userId = req.param('id');
// After
+ const userId = req.params.id;
});References
status-send-order@1.0.0
Migrate legacy res.send(obj, status), res.send(status), res.json(obj, status) and res.jsonp(obj, status)
Migrates usage of the legacy APIs res.send(obj, status), res.json(obj, status), and res.jsonp(obj, status) to use the recommended approach of specifying the status code
using the res.status(status).send(obj), res.status(status).json(obj), and
res.status(status).jsonp(obj) methods respectively. The older APIs that allowed
specifying the status code as a second argument have been deprecated.
Example
Migrating res.send(obj, status)
The migration involves replacing instances of res.send(obj, status) with res.status(status).send(obj).
app.get('/some-route', (req, res) => {
// Some logic here
- res.send(obj, status);
+ res.status(status).send(obj);
});Migrating res.json(obj, status)
The migration involves replacing instances of res.json(obj, status) with res.status(status).json(obj).
app.get('/some-route', (req, res) => {
// Some logic here
- res.json(obj, status);
+ res.status(status).json(obj);
});Migrating res.jsonp(obj, status)
The migration involves replacing instances of res.jsonp(obj, status) with res.status(status).jsonp(obj).
app.get('/some-route', (req, res) => {
// Some logic here
- res.jsonp(obj, status);
+ res.status(status).jsonp(obj);
});Migrating res.send(status)
The migration involves replacing instances of res.send(status) with res.sendStatus(status).
app.get('/some-route', (req, res) => {
// Some logic here
- res.send(status);
+ res.sendStatus(status);
});References
pluralize-method-names@1.0.0
Migrate pluralized request methods
Migrates deprecated request methods to their pluralized versions that were deprecated in Express 4 and removed in Express 5.
Example
Migrating req.acceptsCharset(charset)
The migration involves replacing instances of req.acceptsCharset(charset) with req.acceptsCharsets(charset).
app.get('/', (req, res) => {
- const charset = req.acceptsCharset('utf-8');
+ const charset = req.acceptsCharsets('utf-8');
res.json({ charset });
});Migrating `req.acceptsEncoding(encoding)
The migration involves replacing instances of req.acceptsEncoding(encoding) with req.acceptsEncodings(encoding).
app.get('/', (req, res) => {
- const encoding = req.acceptsEncoding('gzip');
+ const encoding = req.acceptsEncodings('gzip');
res.json({ encoding });
});Migrating req.acceptsLanguage(language)
The migration involves replacing instances of req.acceptsLanguage(language) with req.acceptsLanguages(language).
app.get('/', (req, res) => {
- const language = req.acceptsLanguage('en');
+ const language = req.acceptsLanguages('en');
res.json({ language });
});References
camelcase-sendfile@1.0.0
Migrate legacy res.sendfile(file) to res.sendFile(file)
Migrates usage of the legacy APIs res.sendfile(file) to res.sendFile(file).
Example
Migrating res.sendfile(file)
The migration involves replacing instances of res.sendfile(file) with res.sendFile(file).
app.get('/some-route', (req, res) => {
// Some logic here
- res.sendfile('/path/to/file');
+ res.sendFile('/path/to/file');
});References
back-redirect-deprecated@1.0.0
Migrate legacy res.redirect('back') and res.location('back')
Migrates usage of the legacy APIs res.redirect('back') and res.location('back')
to use the recommended approach of accessing the Referer header directly from
the request object. Versions of Express before 5 allowed the use of the string
"back" as a shortcut to redirect to the referring page, but this has been
deprecated.
Example
Migrating res.redirect('back')
The migration involves replacing instances of res.redirect('back') with res.redirect(req.get('Referer') || '/').
app.get('/some-route', (req, res) => {
// Some logic here
- res.redirect('back');
+ res.redirect(req.get('Referer') || '/');
});Migrating res.location('back')
The migration involves replacing instances of res.location('back') with res.location(req.get('Referer') || '/').
app.get('/some-route', (req, res) => {
// Some logic here
- res.location('back');
+ res.location(req.get('Referer') || '/');
});References
v0.0.5
What's Changed
- chore(deps): bump ossf/scorecard-action from 2.3.1 to 2.4.1 by @dependabot in #40
- chore(deps): bump actions/upload-artifact from 4.3.1 to 4.6.2 by @dependabot in #36
- chore(deps): bump github/codeql-action from 2.23.2 to 3.28.13 by @dependabot in #41
- chore(deps-dev): bump ts-jest from 29.2.5 to 29.3.1 by @dependabot in #46
- chore(deps-dev): bump typescript from 5.6.3 to 5.8.2 by @dependabot in #38
- set version strategy to increase - dependabot by @bjohansebas in #48
- chore(deps-dev): bump typescript from 5.8.2 to 5.8.3 by @dependabot in #50
- chore(deps-dev): bump @types/node from 22.10.1 to 22.14.0 by @dependabot in #51
- ci: harden GH Actions by @UlisesGascon in #55
- fix: conversion of LF to CRLF by @mbtools in #56
- chore(deps-dev): bump @types/node from 22.14.0 to 22.15.3 by @dependabot in #53
- chore(deps-dev): bump ts-jest from 29.3.1 to 29.3.2 by @dependabot in #54
- release: 0.0.5 by @bjohansebas in #59
New Contributors
- @dependabot made their first contribution in #40
- @UlisesGascon made their first contribution in #55
- @mbtools made their first contribution in #56
Full Changelog: v0.0.4...v0.0.5
v0.0.4
What's Changed
- ci: add dependabot configuration by @bjohansebas in #25
- fix bug with res.del when the first arguments it's a function by @bjohansebas in #35
- Release: 0.0.4 by @bjohansebas in #44
Full Changelog: v0.0.3...v0.0.4