獲取請求很中的參數是每個web后台處理的必經之路,nodejs的 express框架 提供了四種方法來實現。
-
req.body
-
req.query
-
req.params
-
req.param()
首先介紹第一個req.body
- <code class="hljs sql" style="">官方文檔解釋:
- Contains key-value pairs of data submitted in the request body. By default, it is undefined,
- and is populated when you <span class="hljs-keyword" style="">use</span> <span class="hljs-keyword" style="">body</span>-parsing middleware such <span class="hljs-keyword" style="">as</span> <span class="hljs-keyword" style="">body</span>-parser <span class="hljs-keyword" style="">and</span> multer.
- 稍微翻譯一下:包含了提交數據的鍵值對在請求的<span class="hljs-keyword" style="">body</span>中,默認是underfined,
- 你可以用<span class="hljs-keyword" style="">body</span>-parser或者multer來解析<span class="hljs-keyword" style="">body</span></code>
解析body不是nodejs默認提供的,你需要載入body-parser中間件才可以使用req.body
此方法通常用來解析POST請求中的數據
第二種是req.query
- <code class="hljs cs" style="">官方文檔解釋:
- An <span class="hljs-keyword" style="">object</span> containing a property <span class="hljs-keyword" style="">for</span> each query <span class="hljs-keyword" style="">string</span> parameter <span class="hljs-keyword" style="">in</span> the route.
- If there <span class="hljs-keyword" style="">is</span> no query <span class="hljs-keyword" style="">string</span>, it <span class="hljs-keyword" style="">is</span> the empty <span class="hljs-keyword" style="">object</span>, {}.
- 翻譯一下:包含在路由中每個查詢字符串參數屬性的對象。如果沒有,默認為{}</code>
有nodejs默認提供,無需載入中間件
舉例說明(官方摘抄):
- <code class="hljs haskell" style="">// <span class="hljs-type" style="">GET</span> /search?q=tobi+ferret
- <span class="hljs-title" style="">req</span>.query.q
- // => <span class="hljs-string" style="">"tobi ferret"</span>
- // <span class="hljs-type" style="">GET</span> /shoes?order=desc&shoe[color]=blue&shoe[<span class="hljs-class" style=""><span class="hljs-keyword" style=""><span class="hljs-class" style=""><span class="hljs-keyword" style="">type</span></span></span><span class="hljs-class" style="">]=converse</span></span>
- <span class="hljs-title" style="">req</span>.query.order
- // => <span class="hljs-string" style="">"desc"</span>
- <span class="hljs-title" style="">req</span>.query.shoe.color
- // => <span class="hljs-string" style="">"blue"</span>
- <span class="hljs-title" style="">req</span>.query.shoe.<span class="hljs-class" style=""><span class="hljs-keyword" style="">type</span></span>
- // => <span class="hljs-string" style="">"converse"</span></code>
此方法多適用於GET請求,解析GET里的參數
第三種是 req.params
- <code class="hljs cs" style="">官方文檔:
- An <span class="hljs-keyword" style="">object</span> containing properties mapped to the named route “parameters”.
- For example, <span class="hljs-keyword" style="">if</span> you have the route /user/:name,
- then the “name” property <span class="hljs-keyword" style="">is</span> available <span class="hljs-keyword" style="">as</span> req.<span class="hljs-keyword" style="">params</span>.name. This <span class="hljs-keyword" style="">object</span> defaults to {}.
- 翻譯:包含映射到指定的路線“參數”屬性的對象。
- 例如,如果你有route/user/:name,那么“name”屬性可作為req.<span class="hljs-keyword" style="">params</span>.name。
- 該對象默認為{}。</code>
nodejs默認提供,無需載入其他中間件
舉例說明
- <code class="hljs cs" style=""><span class="hljs-comment" style="">// GET /user/tj</span>
- req.<span class="hljs-keyword" style="">params</span>.name
- <span class="hljs-comment" style="">// => "tj"</span></code>
多適用於restful風格url中的參數的解析
req.query與req.params的區別
req.params包含路由參數(在URL的路徑部分),而req.query包含URL的查詢參數(在URL的?后的參數)。
最后一種req.param()
此方法被棄用,請看官方解釋
- <code class="hljs css" style=""><span class="hljs-selector-tag" style="">Deprecated</span>. <span class="hljs-selector-tag" style="">Use</span> <span class="hljs-selector-tag" style="">either</span> <span class="hljs-selector-tag" style="">req</span><span class="hljs-selector-class" style="">.params</span>, <span class="hljs-selector-tag" style="">req</span><span class="hljs-selector-class" style="">.body</span> <span class="hljs-selector-tag" style="">or</span> <span class="hljs-selector-tag" style="">req</span><span class="hljs-selector-class" style="">.query</span>, <span class="hljs-selector-tag" style="">as</span> <span class="hljs-selector-tag" style="">applicable</span>.
- 翻譯:被棄用,用其他三種方式替換</code>
取得 GET Request 的 Query Strings:
GET /test?name=fred&tel=0926xxx572 app.get('/test', function(req, res) { console.log(req.query.name); console.log(req.query.tel); });
如果是表單且是用 POST method:
<form action='/test' method='post'> <input type='text' name='name' value='fred'> <input type='text' name='tel' value='0926xxx572'> <input type='submit' value='Submit'> </form> app.post('/test', function(req, res) { console.log(req.query.id); console.log(req.body.name); console.log(req.body.tel); });
當然也可以 Query Strings 和 POST method 的表單同時使用:
<form action='/test?id=3' method='post'> <input type='text' name='name' value='fred'> <input type='text' name='tel' value='0926xxx572'> <input type='submit' value='Submit'> </form> app.post('/test', function(req, res) { console.log(req.query.id); console.log(req.body.name); console.log(req.body.tel); });
順帶補充,還有另一種方法傳遞參數給 Server,就是使用路徑的方式,可以利用 Web Server 的 HTTP Routing 來解析,常見於各種 Web Framework。這不算是傳統標准規范的做法,是屬於 HTTP Routing 的延伸應用。
GET /hello/fred/0926xxx572 app.get('/hello/:name/:tel', function(req, res) { console.log(req.params.name); console.log(req.params.tel); });
來源:http://liuxufei.com/blog/jishu/798.html