@@ -35,10 +35,11 @@ often won't run in the browser.
3535
3636if}}
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
429430downsides to this style of programming—the biggest one being that
430431error handling becomes verbose and error-prone.
431432
432- {{index "Promise class", "promisify function", "util package"}}
433+ {{index "Promise class", "fs/promises package"}}
433434
434435Though 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 ");
444443readFile("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
847846The 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
857855When a requested file does not exist, the correct HTTP status code to
858856return 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"}
863861const {createReadStream} = require("fs");
864- const {stat, readdir} = require("mz/fs ");
862+ const {stat, readdir} = require("fs/promises ");
865863const mime = require("mime");
866864
867865methods.GET = async function(request) {
@@ -886,7 +884,7 @@ methods.GET = async function(request) {
886884
887885Because it has to touch the disk and thus might take a while, ` stat `
888886is 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
891889When 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.
912910The 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
917915methods.DELETE = async function(request) {
918916 let path = urlPath(request.url);
@@ -1083,8 +1081,9 @@ expression object.
10831081{{index "readFileSync function"}}
10841082
10851083Doing 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
11241123of ((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
11291128methods.MKCOL = async function(request) {
11301129 let path = urlPath(request.url);
0 commit comments