Skip to content

Commit 02dd31d

Browse files
committed
Integrate copyediting for Chapter 20
1 parent a74d6b1 commit 02dd31d

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

20_node.md

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ So far, we have used the JavaScript language in a single environment:
2222
the browser. This chapter and the [next one](skillsharing) will
2323
briefly introduce ((Node.js)), a program that allows you to apply your
2424
JavaScript skills outside of the browser. With it, you can build
25-
anything from small command-line tools to HTTP ((server))s that power
25+
anything from small command line tools to HTTP ((server))s that power
2626
dynamic ((website))s.
2727

2828
These chapters aim to teach you the main concepts that Node.js uses
@@ -125,12 +125,12 @@ The `process` binding, just like the `console` binding, is available
125125
globally in Node. It provides various ways to inspect and manipulate
126126
the current program. The `exit` method ends the process and can be
127127
given an exit status code, which tells the program that started `node`
128-
(in this case, the command-line shell) whether the program completed
128+
(in this case, the command line shell) whether the program completed
129129
successfully (code zero) or encountered an error (any other code).
130130

131131
{{index "command line", "argv property"}}
132132

133-
To find the command-line arguments given to your script, you can read
133+
To find the command line arguments given to your script, you can read
134134
`process.argv`, which is an array of strings. Note that it also
135135
includes the name of the `node` command and your script name, so the
136136
actual arguments start at index 2. If `showargv.js` contains the
@@ -166,10 +166,10 @@ program.
166166
{{index [path, "file system"], "relative path", resolution}}
167167

168168
When `require` is called, Node has to resolve the given string to an
169-
actual ((file)) that it can load. Pathnames that start with `"/"`,
170-
`"./"`, or `"../"` are resolved relative to the current module's path,
171-
where `"./"` stands for the current directory, `"../"` for one
172-
directory up, and `"/"` for the root of the file system. So if you ask
169+
actual ((file)) that it can load. Pathnames that start with `/`,
170+
`./`, or `../` are resolved relative to the current module's path,
171+
where `.` stands for the current directory, `../` for one
172+
directory up, and `/` for the root of the file system. So if you ask
173173
for `"./graph"` from the file `/tmp/robot/robot.js`, Node will try to
174174
load the file `/tmp/robot/graph.js`.
175175

@@ -198,7 +198,7 @@ called `main.js`, defines a script that can be called from the
198198
```
199199
const {reverse} = require("./reverse");
200200
201-
// Index 2 holds the first actual command-line argument
201+
// Index 2 holds the first actual command line argument
202202
let argument = process.argv[2];
203203
204204
console.log(reverse(argument));
@@ -207,7 +207,7 @@ console.log(reverse(argument));
207207
{{index reuse, "Array.from function", "join method"}}
208208

209209
The file `reverse.js` defines a library for reversing strings, which
210-
can be used both by this command-line tool and by other scripts that
210+
can be used both by this command line tool and by other scripts that
211211
need direct access to a string-reversing function.
212212

213213
```
@@ -270,7 +270,7 @@ we can call its `parse` property to parse a configuration file.
270270
By default NPM installs packages under the current directory, rather
271271
than in a central place. If you are used to other package managers,
272272
this may seem unusual, but it has advantages—it puts each application
273-
in full control of the packages it installs, and makes it easier to
273+
in full control of the packages it installs and makes it easier to
274274
manage versions and clean up when removing an application.
275275

276276
### Package files
@@ -283,9 +283,9 @@ create such a file for each project, either manually or by running
283283
`npm init`. It contains some information about the project, such as
284284
its name and ((version)), and lists its dependencies.
285285

286-
The robot simulation from [Chapter ?](robot), as modularized in
287-
[Exercise 10.1](modules#modular_robot), might have a `package.json`
288-
file like this:
286+
The robot simulation from [Chapter ?](robot), as modularized in the
287+
exercise in [Chapter ?](modules#modular_robot), might have a
288+
`package.json` file like this:
289289

290290
```{lang: "application/json"}
291291
{
@@ -334,17 +334,17 @@ version, the first number has to be incremented.
334334

335335
A caret character (`^`) in front of the version number for a
336336
dependency in `package.json` indicates that any version compatible
337-
with the given number may be installed. So for example `"^2.3.0"`
337+
with the given number may be installed. So, for example, `"^2.3.0"`
338338
would mean that any version greater than or equal to 2.3.0 and less
339339
than 3.0.0 is allowed.
340340

341341
{{index publishing}}
342342

343343
The `npm` command is also used to publish new packages or new versions
344-
of packages. If you `npm publish` in a ((directory)) that has a
344+
of packages. If you run `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 package name that isn't in use yet, 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
@@ -428,8 +428,8 @@ will retrieve information about a file, `rename` will rename a file,
428428

429429
{{index "asynchronous programming", "Node.js", "error handling", "callback function"}}
430430

431-
And most of these take a callback function as last parameter, which
432-
they call either with an error (the first argument), or a successful
431+
Most of these take a callback function as the last parameter, which
432+
they call either with an error (the first argument) or with a successful
433433
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.
@@ -439,8 +439,8 @@ error handling becomes verbose and error-prone.
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.
441441
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
443-
using promises rather than callback functions.
442+
version 10.1 that contains most of the same functions as `fs` but
443+
uses promises rather than callback functions.
444444

445445
```
446446
const {readFile} = require("fs").promises;
@@ -540,11 +540,11 @@ _localhost_, which would use the default port 80.
540540
When you run this script, the process just sits there and waits. When
541541
a script is listening for events—in this case, network
542542
connections—`node` will not automatically exit when it reaches the end
543-
of the script. To close it, press {{control}}-C.
543+
of the script. To close it, press [control]{keyname}-C.
544544

545545
A real web ((server)) usually does more than the one in the example—it
546546
looks at the request's ((method)) (the `method` property) to see what
547-
action the client is trying to perform and at the request's ((URL)) to
547+
action the client is trying to perform and looks at the request's ((URL)) to
548548
find out which resource this action is being performed on. We'll see a
549549
more advanced server [later in this chapter](node#file_server).
550550

@@ -591,7 +591,7 @@ can be used to make requests to `https:` URLs.
591591

592592
{{index "fetch function", "Promise class", "node-fetch package"}}
593593

594-
But making requests with Node's raw functionality is rather verbose.
594+
Making requests with Node's raw functionality is rather verbose.
595595
There are much more convenient wrapper packages available on NPM. For
596596
example, `node-fetch` provides the promise-based `fetch` interface that
597597
we know from the browser.
@@ -665,7 +665,7 @@ createServer((request, response) => {
665665

666666
The `chunk` value passed to the data handler will be a binary
667667
`Buffer`. We can convert this to a string by decoding it as UTF-8
668-
encoded characters with its `toString` method..
668+
encoded characters with its `toString` method.
669669

670670
The following piece of code, when run with the uppercasing server
671671
active, will send a request to that server and write out the response
@@ -702,7 +702,7 @@ Let's combine our newfound knowledge about ((HTTP)) ((server))s and
702702
working with the ((file system)) to create a bridge between the two:
703703
an HTTP server that allows ((remote access)) to a file system. Such a
704704
server has all kinds of uses—it allows web applications to store and
705-
share data or it can give a group of people shared access to a bunch of
705+
share data, or it can give a group of people shared access to a bunch of
706706
files.
707707

708708
{{index [path, URL], "GET method", "PUT method", "DELETE method"}}
@@ -781,7 +781,7 @@ to be plain text.
781781
When the value of `body` is a ((readable stream)), it will have a
782782
`pipe` method that is used to forward all content from a readable
783783
stream to a ((writable stream)). If not, it is assumed to be either
784-
`null` (no body), a string, or a buffer, and is passed directly to the
784+
`null` (no body), a string, or a buffer, and it is passed directly to the
785785
((response))'s `end` method.
786786

787787
{{index [path, URL], "urlToPath function", "url package", parsing, [escaping, "in URLs"], "decodeURIComponent function", "startsWith method"}}
@@ -816,7 +816,7 @@ system)) to the network.
816816

817817
File paths are strings in Node. To map such a string to an actual
818818
file, there is a nontrivial amount of interpretation going on. Paths
819-
may, for example, include `"../"` to refer to a parent directory. So
819+
may, for example, include `../` to refer to a parent directory. So
820820
one obvious source of problems would be requests for paths like
821821
`/../secret_file`.
822822

@@ -825,7 +825,7 @@ one obvious source of problems would be requests for paths like
825825
To avoid such problems, `urlPath` uses the `resolve` function from the
826826
`path` module, which resolves relative paths. It then verifies that
827827
the result is _below_ the working directory. The `process.cwd`
828-
function (where "cwd" stands for "current working directory") can be
828+
function (where `cwd` stands for "current working directory") can be
829829
used to find this working directory. The `sep` variable from the
830830
`path` package is the system's path separator—a backslash on Windows
831831
and a forward slash on most other systems. When the path doesn't start
@@ -851,7 +851,7 @@ knows the correct type for a large number of ((file extension))s.
851851
{{index "require function", "npm program"}}
852852

853853
The following `npm` command, in the directory where the server script
854-
lives, installs a specific version of `mime`.
854+
lives, installs a specific version of `mime`:
855855

856856
```{lang: null}
857857
$ npm install mime@2.2.0
@@ -894,7 +894,7 @@ is asynchronous. Since we're using promises rather than callback
894894
style, it has to be imported from `promises` instead of directly from
895895
`fs`.
896896

897-
When the file does not exist `stat` will throw an error object with a
897+
When the file does not exist, `stat` will throw an error object with a
898898
`code` property of `"ENOENT"`. These somewhat obscure,
899899
((Unix))-inspired codes are how you recognize error types in Node.
900900

@@ -1006,7 +1006,7 @@ and extend it to solve this chapter's exercises or to experiment.
10061006

10071007
{{index "body (HTTP)", "curl program"}}
10081008

1009-
The command-line tool `curl`, widely available on ((Unix))-like
1009+
The command line tool `curl`, widely available on ((Unix))-like
10101010
systems (such as macOS and Linux), can be used to make ((HTTP))
10111011
((request))s. The following session briefly tests our server. The `-X`
10121012
option is used to set the request's ((method)), and `-d` is used to
@@ -1036,7 +1036,7 @@ Node is a nice, small system that lets us run JavaScript in a
10361036
nonbrowser context. It was originally designed for network tasks to
10371037
play the role of a _node_ in a network. But it lends itself to all
10381038
kinds of scripting tasks, and if writing JavaScript is something you
1039-
enjoy, automating tasks with Node works very well.
1039+
enjoy, automating tasks with Node works well.
10401040

10411041
NPM provides packages for everything you can think of (and quite a few
10421042
things you'd probably never think of), and it allows you to fetch and
@@ -1057,12 +1057,12 @@ callback functions, and Node will call them with an error value and
10571057

10581058
{{index grep, search, "search tool (exercise)"}}
10591059

1060-
On ((Unix)) systems, there is a command-line tool called `grep` that
1060+
On ((Unix)) systems, there is a command line tool called `grep` that
10611061
can be used to quickly search files for a ((regular expression)).
10621062

10631063
Write a Node script that can be run from the ((command line)) and acts
1064-
somewhat like `grep`. It treats its first command-line argument as a
1065-
regular expression, and any further arguments as files to search. It
1064+
somewhat like `grep`. It treats its first command line argument as a
1065+
regular expression and treats any further arguments as files to search. It
10661066
should output the names of any file whose content matches the regular
10671067
expression.
10681068

@@ -1081,7 +1081,7 @@ amount, since most file systems can read only one thing at a time.
10811081

10821082
{{index "RegExp class", "search tool (exercise)"}}
10831083

1084-
Your first command-line argument, the ((regular expression)), can be
1084+
Your first command line argument, the ((regular expression)), can be
10851085
found in `process.argv[2]`. The input files come after that. You can
10861086
use the `RegExp` constructor to go from a string to a regular
10871087
expression object.
@@ -1101,12 +1101,12 @@ To figure out whether something is a directory, you can again use
11011101
{{index "readdir function", "readdirSync function"}}
11021102

11031103
Exploring a directory is a branching process. You can do it either
1104-
with a recursive function or by keeping an array of work (files that
1104+
by using a recursive function or by keeping an array of work (files that
11051105
still need to be explored). To find the files in a directory, you can
1106-
call `readdir` or `readdirSync` (note the strange
1106+
call `readdir` or `readdirSync`. The strange
11071107
capitalization—Node's file system function naming is loosely based on
1108-
standard Unix functions, such as `readdir`, which are all lowercase,
1109-
but then it adds `Sync` with a capital letter).
1108+
standard Unix functions, such as `readdir`, that are all lowercase,
1109+
but then it adds `Sync` with a capital letter.
11101110

11111111
To go from a filename read with `readdir` to a full path name, you
11121112
have to combine it with the name of the directory, putting a ((slash
@@ -1124,7 +1124,7 @@ way to _create_ a ((directory)).
11241124

11251125
{{index "MKCOL method", "mkdir function"}}
11261126

1127-
Add support for a `MKCOL` method ("make column"), which should create
1127+
Add support for the `MKCOL` method ("make column"), which should create
11281128
a directory by calling `mkdir` from the `fs` module. `MKCOL` is not a
11291129
widely used HTTP method, but it does exist for this same purpose in
11301130
the _((WebDAV))_ standard, which specifies a set of conventions on top
@@ -1183,16 +1183,16 @@ website.
11831183

11841184
Use an HTML ((form)) to edit the content of the files that make up the
11851185
website, allowing the user to update them on the server by using HTTP
1186-
requests as described in [Chapter ?](http).
1186+
requests, as described in [Chapter ?](http).
11871187

11881188
Start by making only a single file editable. Then make it so that the
11891189
user can select which file to edit. Use the fact that our file server
11901190
returns lists of files when reading a directory.
11911191

11921192
{{index overwriting}}
11931193

1194-
Don't work directly in the code exposed by the file server, since if
1195-
you make a mistake you are likely to damage the files there. Instead,
1194+
Don't work directly in the code exposed by the file server since if
1195+
you make a mistake, you are likely to damage the files there. Instead,
11961196
keep your work outside of the publicly accessible directory and copy
11971197
it there when testing.
11981198

0 commit comments

Comments
 (0)