Skip to content

docs: clarify MIME type changes when migrating from Express 4 to 5 #1903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions en/guide/migrating-5.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ You can find the list of available codemods [here](https://github.com/expressjs/
<li><a href="#req.param">req.param(name)</a></li>
<li><a href="#res.json">res.json(obj, status)</a></li>
<li><a href="#res.jsonp">res.jsonp(obj, status)</a></li>
<li><a href="#magic-redirect">res.redirect('back') and res.location('back')</a></li>
<li><a href="#magic-redirect">res.redirect('back') and res.location('back')</a></li>
<li><a href="#res.redirect">res.redirect(url, status)</a></li>
<li><a href="#res.send.body">res.send(body, status)</a></li>
<li><a href="#res.send.status">res.send(status)</a></li>
Expand Down Expand Up @@ -317,11 +317,19 @@ app.get('/user', (req, res) => {
res.sendStatus(200)
})
```

<h4 id="res.sendfile">res.sendfile()</h4>

The `res.sendfile()` function has been replaced by a camel-cased version `res.sendFile()` in Express 5.

**Note:** In Express 5, `res.sendFile()` uses the `mime-types` package for MIME type detection, which returns different Content-Type values than Express 4 for several common file types:

- JavaScript files (.js): now "text/javascript" instead of "application/javascript"
- JSON files (.json): now "application/json" instead of "text/json"
- CSS files (.css): now "text/css" instead of "text/plain"
- XML files (.xml): now "application/xml" instead of "text/xml"
- Font files (.woff): now "font/woff" instead of "application/font-woff"
- SVG files (.svg): now "image/svg+xml" instead of "application/svg+xml"

{% include admonitions/note.html content=codemod-deprecated-signatures %}

```js
Expand All @@ -335,7 +343,6 @@ app.get('/user', (req, res) => {
res.sendFile('/path/to/file')
})
```

<h4 id="router.param">router.param(fn)</h4>

The `router.param(fn)` signature was used for modifying the behavior of the `router.param(name, fn)` function. It has been deprecated since v4.11.0, and Express 5 no longer supports it at all.
Expand All @@ -345,6 +352,15 @@ The `router.param(fn)` signature was used for modifying the behavior of the `rou
In Express 5, `mime` is no longer an exported property of the `static` field.
Use the [`mime-types` package](https://github.com/jshttp/mime-types) to work with MIME type values.

**Important:** This change affects not only direct usage of `express.static.mime` but also other Express methods that rely on MIME type detection, such as `res.sendFile()`. The following MIME types have changed from Express 4:

- JavaScript files (.js): now served as "text/javascript" instead of "application/javascript"
- JSON files (.json): now served as "application/json" instead of "text/json"
- CSS files (.css): now served as "text/css" instead of "text/plain"
- HTML files (.html): now served as "text/html; charset=utf-8" instead of just "text/html"
- XML files (.xml): now served as "application/xml" instead of "text/xml"
- Font files (.woff): now served as "font/woff" instead of "application/font-woff"

```js
// v4
express.static.mime.lookup('json')
Expand Down Expand Up @@ -394,7 +410,7 @@ app.get('/*splat', async (req, res) => {
```

{% capture note_wildcard %}
`*splat` matches any path without the root path. If you need to match the root path as well `/`, you can use `/{*splat}`, wrapping the wildcard in braces.
`*splat` matches any path without the root path. If you need to match the root path as well `/`, you can use `/{*splat}`, wrapping the wildcard in braces.

```js
// v5
Expand Down Expand Up @@ -424,15 +440,15 @@ app.get('/:file{.:ext}', async (req, res) => {
app.get('/[discussion|page]/:slug', async (req, res) => {
res.status(200).send('ok')
})
```
```
should be changed to:
```js
app.get(['/discussion/:slug', '/page/:slug'], async (req, res) => {
res.status(200).send('ok')
})
```

- Some characters have been reserved to avoid confusion during upgrade (`()[]?+!`), use `\` to escape them.
- Some characters have been reserved to avoid confusion during upgrade (`()[]?+!`), use `\` to escape them.
- Parameter names now support valid JavaScript identifiers, or quoted like `:"this"`.

<h4 id="rejected-promises">Rejected promises handled from middleware and handlers</h4>
Expand Down Expand Up @@ -463,7 +479,7 @@ const server = app.listen(8080, '0.0.0.0', (error) => {

The `app.router` object, which was removed in Express 4, has made a comeback in Express 5. In the new version, this object is a just a reference to the base Express router, unlike in Express 3, where an app had to explicitly load it.

<h4 id="req.body">req.body</h4>
<h4 id="req.body">req.body</h4>

The `req.body` property returns `undefined` when the body has not been parsed. In Express 4, it returns `{}` by default.

Expand Down
Loading