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
 
 

 



你可能感兴趣的:(express常见获取参数的方法)