You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
stream.on("data", chunk => data += chunk.toString());
325
-
stream.on("end", () => resolve(data));
326
-
});
327
-
}
328
-
```
329
-
330
316
{{index validation, input, "PUT method"}}
331
317
332
318
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
335
321
336
322
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`.
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.
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.
0 commit comments