Node練習 設計路由 取得路徑參數

在上一回,我們透過 params 取得固定網址路徑
這次要來取得網址參數

那我們要怎麼做呢?

這邊會使用 query 取得參數

1
2
3
4
5
6
app.get('/user/:name', (req, res) => {
let {name} = req.params;
let {limit} = req.query;

res.send(`${name} 想要找出前 ${limit ? limit : 0} 筆的購買資料`)
})

我們輸入
http://localhost:3000/user/Mary?limit=27
出現 Mary 想要找出前 27 筆的購買資料

若要串接多筆參數,就使用&串接
忘記可回顧 Node練習 了解網址路徑與路由 了解一下

透過這些參數,後端就可以做出各種不同的篩選功能