Skip to content

Commit d14c299

Browse files
lucleraykodiakhq[bot]chibicode
authored
Add details about req.body helper (#1797)
* add details about req.body * improve a bit how it looks * improve a bit more * Update components/references-mdx/runtimes/official-runtimes/official-runtimes.mdx * Update components/references-mdx/runtimes/official-runtimes/official-runtimes.mdx Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: Shu Uesugi <[email protected]>
1 parent 499af5d commit d14c299

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

components/references-mdx/runtimes/official-runtimes/official-runtimes.mdx

+40-8
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ Each request to a Node.js Serverless Function gives access to Request and Respon
148148

149149
&#8203;<ProductName /> additionally provides helper methods inside of the Request and Response objects passed to Node.js Serverless Functions. These methods are:
150150

151-
| method | description | object |
152-
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
153-
| `req.query` | An object containing the request's [query string](https://en.wikipedia.org/wiki/Query_string), or `{}` if the request does not have a query string. | Request |
154-
| `req.cookies` | An object containing the cookies sent by the request, or `{}` if the request contains no cookies. | Request |
155-
| `req.body` | An object containing the body sent by the request, or `null` if no body is sent. | Request |
156-
| `res.status(code)` | A function to set the status code sent with the response where `code` must be a valid [HTTP status code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes). Returns `res` for chaining. | Response |
157-
| `res.send(body)` | A function to set the content of the response where `body` can be a `string`, an `object` or a `Buffer`. | Response |
158-
| `res.json(obj)` | A function to send a JSON response where `obj` is the JSON object to send. | Response |
151+
| method | description | object |
152+
| ------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
153+
| `req.query` | An object containing the request's [query string](https://en.wikipedia.org/wiki/Query_string), or `{}` if the request does not have a query string. | Request |
154+
| `req.cookies` | An object containing the cookies sent by the request, or `{}` if the request contains no cookies. | Request |
155+
| [`req.body`](#official-runtimes/node-js/node-js-request-and-response-objects/request-body) | An object containing the body sent by the request, or `null` if no body is sent. | Request |
156+
| `res.status(code)` | A function to set the status code sent with the response where `code` must be a valid [HTTP status code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes). Returns `res` for chaining. | Response |
157+
| `res.send(body)` | A function to set the content of the response where `body` can be a `string`, an `object` or a `Buffer`. | Response |
158+
| `res.json(obj)` | A function to send a JSON response where `obj` is the JSON object to send. | Response |
159159

160160
The following Node.js Serverless Function example showcases the use of `req.query`, `req.cookies` and `req.body` helpers:
161161

@@ -207,6 +207,38 @@ We follow a set of rules on the `Content-type` header sent by the request to do
207207

208208
With the `req.body` helper, you can build applications without extra dependencies or having to [parse the content of the request manually](https://nodejs.org/ja/docs/guides/anatomy-of-an-http-transaction/#request-body).
209209

210+
<Note>
211+
The <InlineCode>req.body</InlineCode> helper is set using a{' '}
212+
<Link
213+
tab
214+
href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get"
215+
>
216+
JavaScript getter
217+
</Link>
218+
. In turn, it is only computed when it is accessed.
219+
</Note>
220+
221+
When the request body contains malformed JSON, accessing `req.body` will throw an error. You can catch that error by wrapping `req.body` with [`try...catch`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch):
222+
223+
```js
224+
try {
225+
req.body
226+
} catch (error) {
227+
return res.status(400).json({ error: 'My custom 400 error' })
228+
}
229+
```
230+
231+
<Caption>
232+
Catching the error thrown by <InlineCode>req.body</InlineCode> with{' '}
233+
<Link
234+
tab
235+
href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch"
236+
>
237+
<InlineCode>try...catch</InlineCode>
238+
</Link>
239+
.
240+
</Caption>
241+
210242
---
211243

212244
## Go

0 commit comments

Comments
 (0)