@@ -22,7 +22,7 @@ So far, we have used the JavaScript language in a single environment:
2222the browser. This chapter and the [ next one] ( skillsharing ) will
2323briefly introduce ((Node.js)), a program that allows you to apply your
2424JavaScript 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
2626dynamic ((website))s.
2727
2828These 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
125125globally in Node. It provides various ways to inspect and manipulate
126126the current program. The ` exit ` method ends the process and can be
127127given 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
129129successfully (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
135135includes the name of the ` node ` command and your script name, so the
136136actual arguments start at index 2. If ` showargv.js ` contains the
@@ -166,10 +166,10 @@ program.
166166{{index [ path, "file system"] , "relative path", resolution}}
167167
168168When ` 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
173173for ` "./graph" ` from the file ` /tmp/robot/robot.js ` , Node will try to
174174load the file ` /tmp/robot/graph.js ` .
175175
@@ -198,7 +198,7 @@ called `main.js`, defines a script that can be called from the
198198```
199199const {reverse} = require("./reverse");
200200
201- // Index 2 holds the first actual command- line argument
201+ // Index 2 holds the first actual command line argument
202202let argument = process.argv[2];
203203
204204console.log(reverse(argument));
@@ -207,7 +207,7 @@ console.log(reverse(argument));
207207{{index reuse, "Array.from function", "join method"}}
208208
209209The 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
211211need direct access to a string-reversing function.
212212
213213```
@@ -270,7 +270,7 @@ we can call its `parse` property to parse a configuration file.
270270By default NPM installs packages under the current directory, rather
271271than in a central place. If you are used to other package managers,
272272this 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
274274manage 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
284284its 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
335335A caret character (` ^ ` ) in front of the version number for a
336336dependency 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" `
338338would mean that any version greater than or equal to 2.3.0 and less
339339than 3.0.0 is allowed.
340340
341341{{index publishing}}
342342
343343The ` 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
346346version 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
348348somewhat scary if random people could update existing packages.
349349
350350Since 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
433433result (the second). As we saw in [ Chapter ?] ( async ) , there are
434434downsides to this style of programming—the biggest one being that
435435error handling becomes verbose and error-prone.
@@ -439,8 +439,8 @@ error handling becomes verbose and error-prone.
439439Though promises have been part of JavaScript for a while, at the time
440440of writing their integration into Node.js is still a work in progress.
441441There 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```
446446const {readFile} = require("fs").promises;
@@ -540,11 +540,11 @@ _localhost_, which would use the default port 80.
540540When you run this script, the process just sits there and waits. When
541541a script is listening for events—in this case, network
542542connections—` 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
545545A real web ((server)) usually does more than the one in the example—it
546546looks 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
548548find out which resource this action is being performed on. We'll see a
549549more 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.
595595There are much more convenient wrapper packages available on NPM. For
596596example, ` node-fetch ` provides the promise-based ` fetch ` interface that
597597we know from the browser.
@@ -665,7 +665,7 @@ createServer((request, response) => {
665665
666666The ` 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
670670The following piece of code, when run with the uppercasing server
671671active, 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
702702working with the ((file system)) to create a bridge between the two:
703703an HTTP server that allows ((remote access)) to a file system. Such a
704704server 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
706706files.
707707
708708{{index [ path, URL] , "GET method", "PUT method", "DELETE method"}}
@@ -781,7 +781,7 @@ to be plain text.
781781When 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
783783stream 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
817817File paths are strings in Node. To map such a string to an actual
818818file, 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
820820one 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
825825To avoid such problems, ` urlPath ` uses the ` resolve ` function from the
826826` path ` module, which resolves relative paths. It then verifies that
827827the 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
829829used to find this working directory. The ` sep ` variable from the
830830` path ` package is the system's path separator—a backslash on Windows
831831and 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
853853The 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
894894style, 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
10101010systems (such as macOS and Linux), can be used to make ((HTTP))
10111011((request))s. The following session briefly tests our server. The ` -X `
10121012option 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
10361036nonbrowser context. It was originally designed for network tasks to
10371037play the role of a _ node_ in a network. But it lends itself to all
10381038kinds 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
10411041NPM provides packages for everything you can think of (and quite a few
10421042things 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
10611061can be used to quickly search files for a ((regular expression)).
10621062
10631063Write 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
10661066should output the names of any file whose content matches the regular
10671067expression.
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
10851085found in ` process.argv[2] ` . The input files come after that. You can
10861086use the ` RegExp ` constructor to go from a string to a regular
10871087expression object.
@@ -1101,12 +1101,12 @@ To figure out whether something is a directory, you can again use
11011101{{index "readdir function", "readdirSync function"}}
11021102
11031103Exploring 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
11051105still 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
11071107capitalization—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
11111111To go from a filename read with ` readdir ` to a full path name, you
11121112have 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
11281128a directory by calling ` mkdir ` from the ` fs ` module. ` MKCOL ` is not a
11291129widely used HTTP method, but it does exist for this same purpose in
11301130the _ ((WebDAV))_ standard, which specifies a set of conventions on top
@@ -1183,16 +1183,16 @@ website.
11831183
11841184Use an HTML ((form)) to edit the content of the files that make up the
11851185website, 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
11881188Start by making only a single file editable. Then make it so that the
11891189user can select which file to edit. Use the fact that our file server
11901190returns 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,
11961196keep your work outside of the publicly accessible directory and copy
11971197it there when testing.
11981198
0 commit comments