Skip to content

Commit 1346df0

Browse files
committed
Use node:stream/consumers in chapter 21
1 parent a478ceb commit 1346df0

File tree

1 file changed

+11
-29
lines changed

1 file changed

+11
-29
lines changed

21_skillsharing.md

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ class SkillShareServer {
235235
let fileServer = serveStatic("./public");
236236
this.server = createServer((request, response) => {
237237
serveFromRouter(this, request, response, () => {
238-
fileServer(request, response, notFound);
238+
fileServer(request, response,
239+
() => notFound(request, response));
239240
});
240241
});
241242
}
@@ -312,21 +313,6 @@ router.add("DELETE", talkPath, async (server, title) => {
312313

313314
The `updated` method, which we will define [later](skillsharing#updated), notifies waiting long polling requests about the change.
314315

315-
{{index "readStream function", "body (HTTP)", stream}}
316-
317-
To retrieve the content of a request body, we define a function called `readStream`, which reads all content from a ((readable stream)) and returns a promise that resolves to a string.
318-
319-
```{includeCode: ">code/skillsharing/skillsharing_server.mjs"}
320-
function readStream(stream) {
321-
return new Promise((resolve, reject) => {
322-
let data = "";
323-
stream.on("error", reject);
324-
stream.on("data", chunk => data += chunk.toString());
325-
stream.on("end", () => resolve(data));
326-
});
327-
}
328-
```
329-
330316
{{index validation, input, "PUT method"}}
331317

332318
One handler that needs to read request bodies is the `PUT` handler, which is used to create new ((talk))s. It has to check whether the data it was given has `presenter` and `summary` properties, which are strings. Any data coming from outside the system might be nonsense, and we don't want to corrupt our internal data model or ((crash)) when bad requests come in.
@@ -335,14 +321,16 @@ One handler that needs to read request bodies is the `PUT` handler, which is use
335321

336322
If the data looks valid, the handler stores an object that represents the new talk in the `talks` object, possibly ((overwriting)) an existing talk with this title, and again calls `updated`.
337323

324+
{{index "node:stream/consumers package", JSON, "readable stream"}}
325+
326+
To read the body from the request stream, we will use the `json` function from `"node:stream/consumers"`, which collects the data in the stream and then parses it as JSON. There are similar exports called `text` (to read the content as a string) and `buffer` (to read it as binary data) in this package. Since `json` is a very generic name, the import renames it to `readJSON` to avoid confusion.
327+
338328
```{includeCode: ">code/skillsharing/skillsharing_server.mjs"}
329+
import {json as readJSON} from "node:stream/consumers"
330+
339331
router.add("PUT", talkPath,
340332
async (server, title, request) => {
341-
let requestBody = await readStream(request);
342-
let talk;
343-
try { talk = JSON.parse(requestBody); }
344-
catch (_) { return {status: 400, body: "Invalid JSON"}; }
345-
333+
let talk = await readJSON(request);
346334
if (!talk ||
347335
typeof talk.presenter != "string" ||
348336
typeof talk.summary != "string") {
@@ -359,18 +347,12 @@ router.add("PUT", talkPath,
359347
});
360348
```
361349

362-
{{index validation, "readStream function"}}
363-
364-
Adding a ((comment)) to a ((talk)) works similarly. We use `readStream` to get the content of the request, validate the resulting data, and store it as a comment when it looks valid.
350+
Adding a ((comment)) to a ((talk)) works similarly. We use `readJSON` to get the content of the request, validate the resulting data, and store it as a comment when it looks valid.
365351

366352
```{includeCode: ">code/skillsharing/skillsharing_server.mjs"}
367353
router.add("POST", /^\/talks\/([^\/]+)\/comments$/,
368354
async (server, title, request) => {
369-
let requestBody = await readStream(request);
370-
let comment;
371-
try { comment = JSON.parse(requestBody); }
372-
catch (_) { return {status: 400, body: "Invalid JSON"}; }
373-
355+
let comment = await readJSON(request);
374356
if (!comment ||
375357
typeof comment.author != "string" ||
376358
typeof comment.message != "string") {

0 commit comments

Comments
 (0)