路由規則

網址解析

1
https//www.google.com.tw/search?q=hexschool
  • http:傳輸協定
  • https:傳輸協定加密
  • wwwsub Domain子網域
  • google.com.twDomain網域
  • /search路徑
  • ?q=xxxxParameter參數,會用 ?q 代表,q 代表 query
  • &多個參數會用 & 做串聯。

params 取得指定路徑

透過 request 的參數 params 取得路由的參數

1
2
3
4
5
6
app.get('/news/:id', async(req, res) =>{
//取得動態參數
let params = req.params.id;
console.log(params);
res.send(`<h1>param is ${params}</h1>`);
})

透過 query 取得網址參數

127.0.0.1:3000/page/tom?limit=10&q=ddd

app.get("/page/:name", (req, res) => {
  let params = request.params.name;
  let limit = request.query.limit;
  console.log(limit); // 網址帶入的參數
  let query = request.query.q;
  console.log(query); // 網址帶入的參數
  response.send(
    `<h1>${params} 的播放清單</h1>` +
    `<p>關鍵字為 ${query} 的資料中找出 ${limit} 資料 </p>`
  );
});