Skip to content

Commit 722fb07

Browse files
docs: clarify MIME type changes when migrating from Express 4 to 5 (#1903)
* docs: clarify MIME type changes when migrating from Express 4 to 5 * fix: removed trailing spaces * fix: added router.params(fn) back * docs: specify exact MIME type changes in Express 5 upgrade notes --------- Co-authored-by: Sebastian Beltran <[email protected]>
1 parent 10155e9 commit 722fb07

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

en/guide/migrating-5.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ You can find the list of available codemods [here](https://github.com/expressjs/
5151
<li><a href="#req.param">req.param(name)</a></li>
5252
<li><a href="#res.json">res.json(obj, status)</a></li>
5353
<li><a href="#res.jsonp">res.jsonp(obj, status)</a></li>
54-
<li><a href="#magic-redirect">res.redirect('back') and res.location('back')</a></li>
54+
<li><a href="#magic-redirect">res.redirect('back') and res.location('back')</a></li>
5555
<li><a href="#res.redirect">res.redirect(url, status)</a></li>
5656
<li><a href="#res.send.body">res.send(body, status)</a></li>
5757
<li><a href="#res.send.status">res.send(status)</a></li>
@@ -322,6 +322,15 @@ app.get('/user', (req, res) => {
322322

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

325+
**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:
326+
327+
- JavaScript files (.js): now "text/javascript" instead of "application/javascript"
328+
- JSON files (.json): now "application/json" instead of "text/json"
329+
- CSS files (.css): now "text/css" instead of "text/plain"
330+
- XML files (.xml): now "application/xml" instead of "text/xml"
331+
- Font files (.woff): now "font/woff" instead of "application/font-woff"
332+
- SVG files (.svg): now "image/svg+xml" instead of "application/svg+xml"
333+
325334
{% include admonitions/note.html content=codemod-deprecated-signatures %}
326335

327336
```js
@@ -345,6 +354,15 @@ The `router.param(fn)` signature was used for modifying the behavior of the `rou
345354
In Express 5, `mime` is no longer an exported property of the `static` field.
346355
Use the [`mime-types` package](https://github.com/jshttp/mime-types) to work with MIME type values.
347356

357+
**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:
358+
359+
- JavaScript files (.js): now served as "text/javascript" instead of "application/javascript"
360+
- JSON files (.json): now served as "application/json" instead of "text/json"
361+
- CSS files (.css): now served as "text/css" instead of "text/plain"
362+
- HTML files (.html): now served as "text/html; charset=utf-8" instead of just "text/html"
363+
- XML files (.xml): now served as "application/xml" instead of "text/xml"
364+
- Font files (.woff): now served as "font/woff" instead of "application/font-woff"
365+
348366
```js
349367
// v4
350368
express.static.mime.lookup('json')
@@ -394,7 +412,7 @@ app.get('/*splat', async (req, res) => {
394412
```
395413

396414
{% capture note_wildcard %}
397-
`*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.
415+
`*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.
398416

399417
```js
400418
// v5
@@ -424,15 +442,15 @@ app.get('/:file{.:ext}', async (req, res) => {
424442
app.get('/[discussion|page]/:slug', async (req, res) => {
425443
res.status(200).send('ok')
426444
})
427-
```
445+
```
428446
should be changed to:
429447
```js
430448
app.get(['/discussion/:slug', '/page/:slug'], async (req, res) => {
431449
res.status(200).send('ok')
432450
})
433451
```
434452

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

438456
<h3 id="rejected-promises">Rejected promises handled from middleware and handlers</h3>

0 commit comments

Comments
 (0)