---
# System prepended metadata

title: "\U0001F3C5 Day 25 - req.body"
tags: [2025 Node.js 後端工程師培訓營]

---

# 🏅 Day 25 - req.body
若客戶端請求有 request body（像是 POST 的時候），可使用 req.body 取得 request body 的 `key: value` 資料

範例
```
POST 'http://localhost:3000/posts' 

將資料帶入 request body
{
  title: '貼文標題',
  content: '貼文內容',
  image: 'https://img-url'
}
```

```javascript
app.post('/products', async (req, res, next) => {
  try {
    const data = req.body;  // 取得帶入的資料
    if (data.content) {     // 如果有資料
      const newPost = await Post.create(
        {
          title: data.title,
          content: data.content,
          image: data.image
        }
      );
      res.status(200).json({
        status: 'success',
        message: '新增成功',
      });
    } else {
      console.log("欄位填寫錯誤")
    }
  } catch (error) {
      console.log(error);
  }
});
```
### 參考資源
- [Express - API](https://expressjs.com/zh-tw/api.html)


題目
---
請嘗試透過 POST API 的 req.body 成功時回傳 `${暱稱} 註冊成功` 

```
資料：
{
  nickname: 'Tom',
  email: 'tom@mail.com',
  password: 'abcd1234'
}
```
完整以下程式碼（補上 `...` 的部分）：
```javascript=
app.post('/register', async (req, res) => {
  try {
    const data = ...;
    if (nickname != '' && email != '' && password != '') {
      const newUser = await ...({
        nickname: ...,
        email: ...,
        password: ...
      });
      res.status(200).json({
        status: 'success',
        message: `...`,
      });
    }
  } catch (error) {
    console.log(error);
  }
});
```


## 回報流程

將答案寫在 CodePen 並複製 CodePen 連結貼至底下回報就算完成了喔! 
解答位置請參考下圖（需打開程式碼的部分觀看）

![](https://i.imgur.com/vftL5i0.png)

<!-- 解答：  

app.post('/register', async (req, res) => {
  try {
    const data = req.body;    
    if (data.nickname != '' && data.email != '' && data.password != '') {
      const newUser = await User.create({
        nickname: data.nickname,
        email: data.email,
        password: data.password
      });
      res.status(200).json({
        status: 'success',
        message: `${暱稱} 註冊成功`,
      });
    }
  } catch (error) {
    console.log(error);
  }
});
-->


回報區
---
<!-- 
將答案貼至下方表格內，格式：
| Discord 暱稱 | [CodePen](連結) |
-->
|    Discord    |   CodePen ／ 答案  |
|:-------------:|:-----------------:|
| bian_yang_mofa | [CodePen](https://codepen.io/cssf998811/pen/emYmXJa?editors=0010) |
| Tau | [CodePen](https://codepen.io/Tau-Hsu/pen/NPWPJRB?editors=0010) |
| adengg | [CodePen](https://codepen.io/Osases/pen/raNabPY?editors=0010) |
| poyi | [CodePen](https://codepen.io/poyi-the-flexboxer/pen/yyLyWJP?editors=0010) |
| sian | [CodePen](https://codepen.io/uxitysjl-the-flexboxer/pen/YPzXdmB?editors=0010) |
| suihsilan | [CodePen](https://codepen.io/suihsilan/pen/XJWmdgE?editors=0010) |
| helena | [CodePen](https://codepen.io/helena27/pen/mydePxy) |
|janetlai|[Codepen](https://codepen.io/eiddkqxz-the-builder/pen/zxYvBZQ?editors=0010)
|JC|[Codepen](https://codepen.io/lifetimingwhisper/pen/GgRorjZ)
|ZoeKang| [CodePen](https://codepen.io/byehywmx-the-animator/pen/LEYGmbR?editors=0010)
|Shiang| [CodePen](https://codepen.io/shiang29/pen/wBvzOew?editors=0010)
|HarryKuo|[CodePen](https://codepen.io/harry_kuo/pen/emYBRgd?editors=0010)|
| hsin yu |[CodePen](https://codepen.io/tina2793778/pen/QwWpxJr)|
| li wei-min |[CodePen](https://codepen.io/tina2793778/pen/QwWpxJr)|
|wuyuli_21403|[CodePen](https://codepen.io/Job-Wilhelm/pen/XJWgQNm)|

