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

GraphQL(2) - GraphQL & Apollo client

GraphQL(2) - GraphQL & Apollo client

第七週報名表單檢討

第七週報名表單檢討

Javascript replace space with regex

Javascript replace space with regex


Comments