Skip to content

Commit 008c448

Browse files
committed
Prepare chapter 20 for editing
1 parent fa7ee64 commit 008c448

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

20_node.md

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ If you want to follow along and run the code in this chapter, you'll need to ins
3030

3131
{{index responsiveness, input, [network, speed]}}
3232

33-
One of the more difficult problems with writing systems that communicate over the network is managing input and ((output))—that is, the reading and writing of data to and from the network and ((hard drive)). Moving data around takes time, and ((scheduling)) it cleverly can make a big difference in how quickly a system responds to the user or to network requests.
33+
When building systems that communicate over the network, the way you manage input and ((output))—that is, the reading and writing of data to and from the network and ((hard drive))can make a big difference in how quickly a system responds to the user or to network requests.
3434

3535
{{index ["asynchronous programming", "in Node.js"]}}
3636

3737
In such programs, asynchronous programming is often helpful. It allows the program to send and receive data from and to multiple devices at the same time without complicated thread management and synchronization.
3838

3939
{{index "programming language", "Node.js", standard}}
4040

41-
Node was initially conceived for the purpose of making asynchronous programming easy and convenient. JavaScript lends itself well to a system like Node. It is one of the few programming languages that does not have a built-in way to do in- and output. Thus, JavaScript could be fit onto Node's rather eccentric approach to in- and output without ending up with two inconsistent interfaces. In 2009, when Node was being designed, people were already doing callback-based programming in the browser, so the ((community)) around the language was used to an asynchronous programming style.
41+
Node was initially conceived for the purpose of making asynchronous programming easy and convenient. JavaScript lends itself well to a system like Node. It is one of the few programming languages that does not have a built-in way to do in- and output. Thus, JavaScript could be fit onto Node's rather eccentric approach to network and file system programming without ending up with two inconsistent interfaces. In 2009, when Node was being designed, people were already doing callback-based programming in the browser, so the ((community)) around the language was used to an asynchronous programming style.
4242

4343
## The node command
4444

@@ -271,7 +271,7 @@ Here it was not necessary to specify the encoding—`writeFile` will assume that
271271

272272
{{index "node:fs package", "readdir function", "stat function", "rename function", "unlink function"}}
273273

274-
The `node:fs` module contains many other useful functions: `readdir` will return the files in a ((directory)) as an array of strings, `stat` will retrieve information about a file, `rename` will rename a file, `unlink` will remove one, and so on. See the documentation at [_https://nodejs.org_](https://nodejs.org) for specifics.
274+
The `node:fs` module contains many other useful functions: `readdir` will give you the files in a ((directory)) as an array of strings, `stat` will retrieve information about a file, `rename` will rename a file, `unlink` will remove one, and so on. See the documentation at [_https://nodejs.org_](https://nodejs.org) for specifics.
275275

276276
{{index ["asynchronous programming", "in Node.js"], "Node.js", "error handling", "callback function"}}
277277

@@ -305,7 +305,7 @@ Do note that while such a synchronous operation is being performed, your program
305305

306306
{{index "Node.js", "node:http package", [HTTP, server]}}
307307

308-
Another central module is called `node:http`. It provides functionality for running HTTP ((server))s and making HTTP ((request))s.
308+
Another central module is called `node:http`. It provides functionality for running an HTTP ((server)).
309309

310310
{{index "listening (TCP)", "listen method", "createServer function"}}
311311

@@ -356,7 +356,7 @@ A real web ((server)) usually does more than the one in the example—it looks a
356356

357357
{{index "node:http package", "request function", "fetch function", [HTTP, client]}}
358358

359-
The `node:http` module also provides a `request` function, which can be used to make HTTP requests. However, it is a lot more cumbersome to use than `fetch`, which we saw in [Chapter ?](http). Fortunately, `fetch` is also available in Node, as a global binding. Unless you want to do something very specific, such as processing the response document piece by piece, as the data comes in over the network, I recommend sticking to `fetch`.
359+
The `node:http` module also provides a `request` function, which can be used to make HTTP requests. However, it is a lot more cumbersome to use than `fetch`, which we saw in [Chapter ?](http). Fortunately, `fetch` is also available in Node, as a global binding. Unless you want to do something very specific, such as processing the response document piece by piece as the data comes in over the network, I recommend sticking to `fetch`.
360360

361361
## Streams
362362

@@ -366,7 +366,7 @@ The response object that the HTTP server could write to is an example of a _writ
366366

367367
{{index "createWriteStream function", "writeFile function", [file, stream]}}
368368

369-
It is possible to create a writable stream that points at a file with the `createWriteStream` function from the `node:fs` module. You can use the `write` method on the resulting object to write the file one piece at a time, rather than in one shot as with `writeFile`.
369+
It is possible to create a writable stream that points at a file with the `createWriteStream` function from the `node:fs` module. You can then use the `write` method on the resulting object to write the file one piece at a time, rather than in one shot as with `writeFile`.
370370

371371
{{index "createServer function", "request function", "event handling", "readable stream"}}
372372

@@ -435,16 +435,14 @@ const methods = Object.create(null);
435435
436436
createServer((request, response) => {
437437
let handler = methods[request.method] || notAllowed;
438-
handler(request)
439-
.catch(error => {
440-
if (error.status != null) return error;
441-
return {body: String(error), status: 500};
442-
})
443-
.then(({body, status = 200, type = "text/plain"}) => {
444-
response.writeHead(status, {"Content-Type": type});
445-
if (body && body.pipe) body.pipe(response);
446-
else response.end(body);
447-
});
438+
handler(request).catch(error => {
439+
if (error.status != null) return error;
440+
return {body: String(error), status: 500};
441+
}).then(({body, status = 200, type = "text/plain"}) => {
442+
response.writeHead(status, {"Content-Type": type});
443+
if (body && body.pipe) body.pipe(response);
444+
else response.end(body);
445+
});
448446
}).listen(8000);
449447
450448
async function notAllowed(request) {
@@ -545,7 +543,7 @@ methods.GET = async function(request) {
545543

546544
{{index "createReadStream function", ["asynchronous programming", "in Node.js"], "error handling", "ENOENT (status code)", "Error type", inheritance}}
547545

548-
Because it has to touch the disk and thus might take a while, `stat` is asynchronous. Since we're using promises rather than callback style, it has to be imported from `promises` instead of directly from `node:fs`.
546+
Because it has to touch the disk and thus might take a while, `stat` is asynchronous. Since we're using promises rather than callback style, it has to be imported from `node:fs/promises` instead of directly from `node:fs`.
549547

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

@@ -673,7 +671,7 @@ Your first command line argument, the ((regular expression)), can be found in `p
673671

674672
{{index "readFileSync function"}}
675673

676-
Doing this synchronously, with `readFileSync`, is more straightforward, but if you use `fs/promises` to get promise-returning functions and write an `async` function, the code looks similar.
674+
Doing this synchronously, with `readFileSync`, is more straightforward, but if you use `node:fs/promises` to get promise-returning functions and write an `async` function, the code looks similar.
677675

678676
{{index "stat function", "statSync function", "isDirectory method"}}
679677

@@ -683,7 +681,7 @@ To figure out whether something is a directory, you can again use `stat` (or `st
683681

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

686-
To go from a filename read with `readdir` to a full path name, you have to combine it with the name of the directory, either putting `sep` from `node:path` between them, or using `join` from that same package.
684+
To go from a filename read with `readdir` to a full path name, you have to combine it with the name of the directory, either putting `sep` from `node:path` between them, or using the `join` function from that same package.
687685

688686
hint}}
689687

@@ -727,7 +725,7 @@ hint}}
727725

728726
{{index "public space (exercise)", "file server example", "Content-Type header", website}}
729727

730-
Since the file server serves up any kind of file and even includes the right `Content-Type` header, you can use it to serve a website. Since it allows everybody to delete and replace files, it would be an interesting kind of website: one that can be modified, improved, and vandalized by everybody who takes the time to create the right HTTP request.
728+
Since the file server serves up any kind of file and even includes the right `Content-Type` header, you can use it to serve a website. Since it allows everybody to delete and replace files, it would be an interesting kind of website: one that can be modified, improved, and vandalized by everybody who takes the time to make the right HTTP request.
731729

732730
Write a basic ((HTML)) page that includes a simple JavaScript file. Put the files in a directory served by the file server and open them in your browser.
733731

0 commit comments

Comments
 (0)