Skip to content

Commit 6fcecf8

Browse files
committed
Make Chapter 20 use Node 10's fs/promises module
Instead of mz
1 parent 47471cb commit 6fcecf8

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

20_node.md

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ often won't run in the browser.
3535

3636
if}}
3737

38-
If you want to follow along and run the code in this chapter, start by
39-
going to [_nodejs.org_](https://nodejs.org) and following the
40-
installation instructions for your operating system. You can also find
41-
further ((documentation)) for Node.js there.
38+
If you want to follow along and run the code in this chapter, you'll
39+
need to install Node.js version 10 or higher. To do so, go to
40+
[_nodejs.org_](https://nodejs.org) and follow the installation
41+
instructions for your operating system. You can also find further
42+
((documentation)) for Node.js there.
4243

4344
## Background
4445

@@ -429,18 +430,16 @@ result (the second). As we saw in [Chapter ?](async), there are
429430
downsides to this style of programming—the biggest one being that
430431
error handling becomes verbose and error-prone.
431432

432-
{{index "Promise class", "promisify function", "util package"}}
433+
{{index "Promise class", "fs/promises package"}}
433434

434435
Though promises have been part of JavaScript for a while, at the time
435-
of writing the work to integrate them in Node.js is still in progress.
436-
There is a `promisify` function in the built-in `util` module, which
437-
you can call on a callback-style function to create a
438-
promise-returning function. But since this is a little awkward, there
439-
are also packages on NPM like `mz`, which provide their own,
440-
promise-style version of built-in Node modules.
436+
of writing their integration into Node.js is still a work in progress.
437+
There is a package called `fs/promises` in the standard library since
438+
version 10, which exports most of the same functions as `fs`, but
439+
using promises rather than callback functions.
441440

442441
```
443-
const {readFile} = require("mz/fs");
442+
const {readFile} = require("fs/promises");
444443
readFile("file.txt", "utf8")
445444
.then(text => console.log("The file contains:", text));
446445
```
@@ -845,14 +844,13 @@ knows the correct type for a large number of ((file extension))s.
845844
{{index "require function", "npm program"}}
846845

847846
The following `npm` command, in the directory where the server script
848-
lives, installs specific versions of `mime` and `mz`. We'll use the
849-
latter as a source of promise-based `fs` functions.
847+
lives, installs a specific version of `mime`.
850848

851849
```{lang: null}
852-
$ npm install mime@2.2.0 mz@2.7.0
850+
$ npm install mime@2.2.0
853851
```
854852

855-
{{index "404 (HTTP status code)", "stat function", "mz package"}}
853+
{{index "404 (HTTP status code)", "stat function"}}
856854

857855
When a requested file does not exist, the correct HTTP status code to
858856
return is 404. We'll use the `stat` function, which looks up
@@ -861,7 +859,7 @@ and whether it is a ((directory)).
861859

862860
```{includeCode: ">code/file_server.js"}
863861
const {createReadStream} = require("fs");
864-
const {stat, readdir} = require("mz/fs");
862+
const {stat, readdir} = require("fs/promises");
865863
const mime = require("mime");
866864
867865
methods.GET = async function(request) {
@@ -886,7 +884,7 @@ methods.GET = async function(request) {
886884

887885
Because it has to touch the disk and thus might take a while, `stat`
888886
is asynchronous. Since we're using promises rather than callback
889-
style, it has to be imported from `mz/fs` instead of `fs`.
887+
style, it has to be imported from `fs/promises` instead of `fs`.
890888

891889
When the file does not exist `stat` will throw an error object with a
892890
`code` property of `"ENOENT"`. These somewhat obscure,
@@ -912,7 +910,7 @@ content type that the `mime` package gives us for the file's name.
912910
The code to handle `DELETE` requests is slightly simpler.
913911

914912
```{includeCode: ">code/file_server.js"}
915-
const {rmdir, unlink} = require("mz/fs");
913+
const {rmdir, unlink} = require("fs/promises");
916914
917915
methods.DELETE = async function(request) {
918916
let path = urlPath(request.url);
@@ -1083,8 +1081,9 @@ expression object.
10831081
{{index "readFileSync function"}}
10841082

10851083
Doing this synchronously, with `readFileSync`, is more
1086-
straightforward, but if you use `mz` again to get promise-returning
1087-
functions and write an `async` function, the code looks similar.
1084+
straightforward, but if you use `fs/promises` again to get
1085+
promise-returning functions and write an `async` function, the code
1086+
looks similar.
10881087

10891088
{{index "stat function", "statSync function", "isDirectory method"}}
10901089

@@ -1124,7 +1123,7 @@ the _((WebDAV))_ standard, which specifies a set of conventions on top
11241123
of ((HTTP)) that make it suitable for creating documents.
11251124

11261125
```{hidden: true, includeCode: ">code/file_server.js"}
1127-
const {mkdir} = require("mz/fs");
1126+
const {mkdir} = require("fs/promises");
11281127
11291128
methods.MKCOL = async function(request) {
11301129
let path = urlPath(request.url);

code/solutions/20_2_directory_creation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This code won't work on its own, but is also included in the
22
// code/file_server.js file, which defines the whole system.
33

4-
const {mkdir} = require("mz/fs");
4+
const {mkdir} = require("fs/promises");
55

66
methods.MKCOL = async function(request) {
77
let path = urlPath(request.url);

src/chapter_info.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let fs = require("fs");
77

88
let output = [], failed = false;
99

10-
let allSolutions = fs.readdirSync("code/solutions/").filter(file => !/^2[012]/.test(file));
10+
let allSolutions = fs.readdirSync("code/solutions/").filter(file => !/^(\.|2[012])/.test(file));
1111

1212
for (let file of fs.readdirSync(".").sort()) {
1313
let match = /^((\d+).*).md$/.exec(file), chapNum = match && match[2];

0 commit comments

Comments
 (0)