Express取得網址內的資訊,幾種不同方式


Posted by mijouhsieh on 2023-04-06

Method 1 : 透過在路由上給一個變數(params),讓server知道user在網址列輸入了什麼。
  • 把預計讓user"動態輸入內容的部分",以變數代替。
    動態路由:在路由中使用的變數 :變數,稱params。
    app.get('/coffeeTypes/:coffee', (req,res)=> {
      //...
      })
    

`:coffee` => 動態路由 params
  • 取得user在路由中輸入的內容(變數params):
(1)req {
  (2)params: {(3)coffee: (4)"值";}
  }

(1) req object 請求物件
(2) params物件
(3) 定義路由中的變數(params)
(4) user在url該變數位置輸入的值(內容)。
console.log(req(物件).params(物件).coffee(變數key)) =>取得動態路由中user輸入的內容。



#get-data-from-url







Related Posts

不要動!把資料交出來(3) - GET & POST

不要動!把資料交出來(3) - GET & POST

D6_第一週複習、直播影片檢討

D6_第一週複習、直播影片檢討

[ 學習筆記系列 ] 後端基礎 (全) - MySQL 語法、基礎 PHP 與 Session / Cookie

[ 學習筆記系列 ] 後端基礎 (全) - MySQL 語法、基礎 PHP 與 Session / Cookie


Comments