---
# System prepended metadata

title: node.js第六章 express
tags: [node.js, 第六章]

---

---
tags: node.js,第六章
---
# node.js第六章 express
```javascript=
// 導入
const express = require('express');
// 創建
const app = express();
// 啟動
app.listen(80, () => {
  console.log('express server running at http://127.0.0.1');
});
// 監聽客戶請求
app.get('/user', (req, res) => {
  res.send({ name: 'zs', age: 20, gender: '男' });
});
app.post('/user', (req, res) => {
  res.send('請求成功');
});
app.get('/', (req, res) => {
  // 默認req.query是空對象
  console.log(req.query);
  res.send(req.query);
});
// :id是動態參數，可以有多個
// 範例寫法http://127.0.0.1/001/pink
// 路由設定的動態參數有幾個就要傳入幾個，絕不可多不可少
app.get('/user/:id/:name', (req, res) => {
  // req.params是動態匹配url參數，默認也是空對象
  console.log(req.params);
  res.send(req.params);
});
// 對外提供靜態資源
app.use(express.static('./public'))
```
![](https://i.imgur.com/ULS117A.jpg)
![](https://i.imgur.com/UhyTITL.jpg)
![](https://i.imgur.com/5qiNOIK.jpg)
![](https://i.imgur.com/OUbXloA.jpg)
![](https://i.imgur.com/gg3j8Jk.jpg)
![](https://i.imgur.com/aYvCgX9.jpg)
![](https://i.imgur.com/YDYw4QV.jpg)
![](https://i.imgur.com/ZEroMEJ.jpg)
![](https://i.imgur.com/o7c0CRb.jpg)
![](https://i.imgur.com/d2UckwN.jpg)
![](https://i.imgur.com/zhi8HNS.jpg)
![](https://i.imgur.com/dCKY0mA.jpg)
```
注意當2個文件夾都有相同檔名的文件，獲取時只會找程式碼順序較前資料夾的文件
```
![](https://i.imgur.com/GTe6Kks.jpg)
https://ithelp.ithome.com.tw/articles/10193307
![](https://i.imgur.com/vx9Ul39.jpg)
![](https://i.imgur.com/pskGQ3N.jpg)
![](https://i.imgur.com/AxJDp2e.jpg)