---
tags: Node.js 直播班 - 2022 春季班
---
# 🏅 Day 15
## req.body
若客戶端請求有 request body,可使用 req.body 取得 request body 的 `key: value` 資料
範例
```
POST 'http://localhost:3000/products'
將資料帶入 request body
{
"product": "hamburger",
"quantity": 2,
"price": 50
}
```
```javascript
app.post('/products', async function(req, res, next) {
try {
const data = req.body;
if (data.product) {
// 使用 mongoose 將 data 新增至資料庫
const newProduct = await Product.create(
{
product: data.product,
quantity: data.quantity,
price: data.price
}
);
res.status(200).json({
status: 'success',
data,
});
} else {
console.log("欄位填寫錯誤")
}
} catch (error) {
console.log(error)
}
});
```
### 參考資源
[Express - API](https://expressjs.com/zh-tw/api.html)
### 題目(將答案寫在 **GitHub Repo** 並提交至回報區)
請參考[最終作業設計稿](https://xd.adobe.com/view/c0763dbe-fc15-42e8-be0b-8956ed03e675-9525/grid),透過 POST API 的 req.body 新增一則貼文,並加上 Schema 欄位資料驗證,可複製或下載此[程式碼](https://github.com/dogwantfly/express-add-post)練習,完成後提交自己的 GitHub Repo
回報流程
---
請同學依照下圖教學觀看解答、回報答案:

回報格式:請在「回報區」貼上 CodePen 或 HackMD 連結回報答案 (為了統計人數,請同學依序加上「報數」)
<!-- 解答
範例參考(可自行優化程式碼)
```javascript
router.post('/', async(req, res, next) => {
try{
const data = req.body;
if(data.content !== undefined){
const newPost = await Post.create(
{
name: data.name,
content: data.content,
tags:data.tags,
type:data.type
}
);
res.status(200).json({
status: 'success',
"data": newPost
});
}else{
res.status(400).json({
"status": 'false',
"message": "欄位未填寫正確,或無此 ID"
});
}
}catch (error){
res.status(400).json({
status: 'false',
"message": "欄位未填寫正確,或無此 ID"
});
}
})
```
-->
回報區
---
| 報數 | 組別 / 名字 | codepen / hackMD / 其他回饋 |
| ---- | -------------------- | --------------------------------------------------------------------- |
| 1 | 第 6 組/ Wendy | [HackMD](https://hackmd.io/@0k_MSPdgRPujozP6w_D-bA/rksuDvDHq) |
| 2 | 第 2 組 / joe.chang | [HackMD](https://hackmd.io/uaRTw9c0TgOrADyXMmGL0Q?view#428---reqbody) |
| 3 | 第 4 組 / 小宥 | [HackMD](https://hackmd.io/inGmSsd3QoO7GKxUq1wX_A?both) |
| 4 | 第 3 組 / Hobby | [HackMD](https://hackmd.io/@hobbyling/day15) |
| 5 | 第 2 組 / wendy.li | [HackMD](https://hackmd.io/3Y9dXvgSRT6ptfum6xMiFQ) |
| 6 | 第 3 組 / HedgehogKU | [HackMD](https://hackmd.io/d-lRv1EVTdKMPm2e0R5T4w) |
| 7 | 第 14 組|East | [HackMD](https://hackmd.io/HN9FaH0sTuiZ69uJtDB7CQ) |
| 8 | 第 11 組|Han Lai | [HackMD](https://hackmd.io/D9m1v63fTzKnCv52NzZ8Gw?view) |
| 9 | 第 3 組|hiYifang | [HackMD](https://hackmd.io/@gPeowpvtQX2Om6AmD-s3xw/HkiSA6Jrc) |
| 10 | 第 8 組|Hank | [HackMD](https://hackmd.io/@hank-hsiao/BkjOqdPBc) |
| 11 | 第 3 組|Justin | [HackMD](https://hackmd.io/D08h3gTuT3KKTFZ26dzo1Q) |
| 12 | 第 9 組 / 黃士桓 | [HackMD](/vvApLVD1SLe8Cjb474o30w) |
|13 |第 9 組 / konstante |[HackMD](https://hackmd.io/w4OdAGPiRuaWT-cwo_FDkQ?edit) |
|14 |第 4 組 / 苡安 |[HackMD](https://hackmd.io/N-jD4NxLQiST4iuh9nkICg?both) |
|15 |第 5 組 / Hazel |[HackMD@Hazel](https://hackmd.io/@hazelwu/day15) |
|16 |Naiky |[HackMD](https://hackmd.io/@UWBC7rrORiKaLSBg226mZg/SyKhAYDHc) |
|17|第13組/KFC|[HackMD](https://hackmd.io/N9ozc_hmRWWmSrz0Uepi-Q?view)|
| 18 | 第 4 組 / sihle | [HackMD](https://hackmd.io/@bugbug777/BJd3EB_Bq) |
| 19 | 第 2 組 / peter | [HackMD](https://hackmd.io/@peterchen1024/rJ4bAzYB5) |
| 20 | 第 14 組 / Uniza | [HackMD](https://hackmd.io/nsREp_HNTL2bSZdlan4aNA)
| 21 | 第 1 組 / Claire | [HackMD](https://hackmd.io/cu9lX3yBReGgP4jxcBEkGQ)
| 22 | 第 11 組 / Han Lai | [HackMD](https://hackmd.io/D9m1v63fTzKnCv52NzZ8Gw)
| 23 | 第 2 組 / Rikkubook | [GitHub](https://github.com/Rikkubook/Node-post)
| 24 | 第 1 組 / snow | [HackMD](https://hackmd.io/@snowsuika/rymhtsiH5)
| 25 | 第 7 組 / jason06286 | [HackMD](https://hackmd.io/Or1Tfri_ROuiADZh9QXLuw)
| 26 | 第 12 組 / Jimmy | [HackMD](https://hackmd.io/HRJRprogTaKsgZuUBRQ-0A)
| 27 | 第 10 組 / 橘子 | [GitHub](https://github.com/WeiweiJoanne/Day15)
| 28 | 第 16 組 / 皓皓 | [GitHub](https://hackmd.io/QTAOXxifQJu3y4lq39lpNw)
| 29 | 第 9 組 / Reynold | [HackMD](https://hackmd.io/@dL7AuQEMQ6KpCSRHaYElOA/r1V63t0Hq)
| 30 | 第 5 組 / Nap | [Github](https://github.com/rainingut/hex-daily-task-15_express_req.body)
| 31 | 第 1 組 / Ed Huang | [HackMD](https://hackmd.io/rFegbqluQOKZsJhreX4QoQ?view)
| 32 | nick | [HackMD](https://hackmd.io/ZLL8OgVnQKGnSwIf9jWKiQ)
| 33 | mandy | [HackMD](https://hackmd.io/-MJiUT5uQeKo5Ry2j9ozUg?view)
| 34 | 第 3 組 / 小葉 | [HackMD](https://hackmd.io/@FyKv37KcRSWqAO_e336w8g/rk4E5i1Uc)
| 35 | 第 15 組 / yolala | [HackMD](https://hackmd.io/TNKbw5YzTn-bd14pW4tvhw)