Skip to content

Commit fb0a429

Browse files
committed
Integrate tech editing for Chapter 20
1 parent 7d6ebb4 commit fb0a429

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

20_node.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ often won't run in the browser.
4040
if}}
4141

4242
If you want to follow along and run the code in this chapter, you'll
43-
need to install Node.js version 10 or higher. To do so, go to
43+
need to install Node.js version 10.1 or higher. To do so, go to
4444
[_https://nodejs.org_](https://nodejs.org) and follow the installation
4545
instructions for your operating system. You can also find further
4646
((documentation)) for Node.js there.
@@ -151,8 +151,8 @@ Browser-related functionality, such as `document` or `prompt`, is not.
151151

152152
{{index "Node.js", "global scope", "module loader"}}
153153

154-
Beyond the few bindings I mentioned, such as `console` and `process`,
155-
Node puts few bindings in the global scope. If you want to access
154+
Beyond the bindings I mentioned, such as `console` and `process`,
155+
Node puts few additional bindings in the global scope. If you want to access
156156
built-in functionality, you have to ask the module system for it.
157157

158158
{{index "require function"}}
@@ -344,7 +344,7 @@ The `npm` command is also used to publish new packages or new versions
344344
of packages. If you `npm publish` in a ((directory)) that has a
345345
`package.json` file, it will publish a package with the name and
346346
version listed in the JSON file to the registry. Anyone can publish
347-
packages to NPM—though only under a new name, since it would be
347+
packages to NPM—though only under a package name that isn't in use yet, since it would be
348348
somewhat scary if random people could update existing packages.
349349

350350
Since the `npm` program is a piece of software that talks to an open
@@ -434,16 +434,16 @@ result (the second). As we saw in [Chapter ?](async), there are
434434
downsides to this style of programming—the biggest one being that
435435
error handling becomes verbose and error-prone.
436436

437-
{{index "Promise class", "fs/promises package"}}
437+
{{index "Promise class", "promises package"}}
438438

439439
Though promises have been part of JavaScript for a while, at the time
440440
of writing their integration into Node.js is still a work in progress.
441-
There is a package called `fs/promises` in the standard library since
442-
version 10, which exports most of the same functions as `fs`, but
441+
There is an object `promises` exported from the `fs` package since
442+
version 10.1, which contains most of the same functions as `fs`, but
443443
using promises rather than callback functions.
444444

445445
```
446-
const {readFile} = require("fs/promises");
446+
const {readFile} = require("fs").promises;
447447
readFile("file.txt", "utf8")
448448
.then(text => console.log("The file contains:", text));
449449
```
@@ -489,6 +489,7 @@ let server = createServer((request, response) => {
489489
response.end();
490490
});
491491
server.listen(8000);
492+
console.log("Listening! (port 8000)");
492493
```
493494

494495
{{index port, localhost}}
@@ -863,7 +864,7 @@ and whether it is a ((directory)).
863864

864865
```{includeCode: ">code/file_server.js"}
865866
const {createReadStream} = require("fs");
866-
const {stat, readdir} = require("fs/promises");
867+
const {stat, readdir} = require("fs").promises;
867868
const mime = require("mime");
868869
869870
methods.GET = async function(request) {
@@ -888,7 +889,8 @@ methods.GET = async function(request) {
888889

889890
Because it has to touch the disk and thus might take a while, `stat`
890891
is asynchronous. Since we're using promises rather than callback
891-
style, it has to be imported from `fs/promises` instead of `fs`.
892+
style, it has to be imported from `promises` instead of directly from
893+
`fs`.
892894

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

916918
```{includeCode: ">code/file_server.js"}
917-
const {rmdir, unlink} = require("fs/promises");
919+
const {rmdir, unlink} = require("fs").promises;
918920
919921
methods.DELETE = async function(request) {
920922
let path = urlPath(request.url);
@@ -1085,7 +1087,7 @@ expression object.
10851087
{{index "readFileSync function"}}
10861088

10871089
Doing this synchronously, with `readFileSync`, is more
1088-
straightforward, but if you use `fs/promises` again to get
1090+
straightforward, but if you use `fs.promises` again to get
10891091
promise-returning functions and write an `async` function, the code
10901092
looks similar.
10911093

@@ -1127,7 +1129,7 @@ the _((WebDAV))_ standard, which specifies a set of conventions on top
11271129
of ((HTTP)) that make it suitable for creating documents.
11281130

11291131
```{hidden: true, includeCode: ">code/file_server.js"}
1130-
const {mkdir} = require("fs/promises");
1132+
const {mkdir} = require("fs").promises;
11311133
11321134
methods.MKCOL = async function(request) {
11331135
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("fs/promises");
4+
const {mkdir} = require("fs").promises;
55

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"markdown-it": "^8.4.0",
1717
"markdown-it-sub": "^1.0.0",
1818
"markdown-it-sup": "^1.0.0",
19+
"mime": "^2.3.1",
1920
"mold-template": "^2.0.1",
2021
"uglify-js": "^2.0.0"
2122
},

0 commit comments

Comments
 (0)