express常見獲取參數的方法


1、req.query 處理get請求

// GET /search?q=tobi+ferret req.query.q // => "tobi ferret" // GET /shoes?order=desc&shoe[color]=blue&shoe[type]=converse req.query.order // => "desc" req.query.shoe.color // => "blue" req.query.shoe.type // => "converse"

2、req.body 處理post請求

// post /search?q=tobi+ferret req.body.q // => "tobi ferret" // post /shoes?order=desc&shoe[color]=blue&shoe[type]=converse req.body.order // => "desc" req.body.shoe.color // => "blue" req.body.shoe.type // => "converse"

3、req.params

// GET /user/tj req.params.name // => "tj"


// GET /file/javascripts/jquery.js req.params[0] // => "javascripts/jquery.js"

4、req.param(name [, defaultValue])

// ?name=tobi req.param('name') // => "tobi" // POST name=tobi req.param('name') // => "tobi" // /user/tobi for /user/:name req.param('name') // => "tobi"

Lookup is performed in the following order:

  • req.params
  • req.body
  • req.query
 
 

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM