摘要: nodejs取参四种方法req.body,req.params,req.param,req.body 获取请求很中的参数是每个web后台处理的必经之路,nodejs提供了四种方法来实现。

获取请求很中的参数是每个web后台处理的必经之路,nodejs的 express框架 提供了四种方法来实现。

  1. req.body

  2. req.query

  3. req.params

  4. req.param()

首先介绍第一个req.body

  1. <code class="hljs sql" style="">官方文档解释:
  2. Contains key-value pairs of data submitted in the request body. By default, it is undefined,
  3. 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.
  4. 稍微翻译一下:包含了提交数据的键值对在请求的<span class="hljs-keyword" style="">body</span>中,默认是underfined,
  5. 你可以用<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

  1. <code class="hljs cs" style="">官方文档解释:
  2. 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.
  3. 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>, {}.
  4. 翻译一下:包含在路由中每个查询字符串参数属性的对象。如果没有,默认为{}</code>

有nodejs默认提供,无需载入中间件

举例说明(官方摘抄):

  1. <code class="hljs haskell" style="">// <span class="hljs-type" style="">GET</span> /search?q=tobi+ferret
  2. <span class="hljs-title" style="">req</span>.query.q
  3. // => <span class="hljs-string" style="">"tobi ferret"</span>
  4. // <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>
  5. <span class="hljs-title" style="">req</span>.query.order
  6. // => <span class="hljs-string" style="">"desc"</span>
  7. <span class="hljs-title" style="">req</span>.query.shoe.color
  8. // => <span class="hljs-string" style="">"blue"</span>
  9. <span class="hljs-title" style="">req</span>.query.shoe.<span class="hljs-class" style=""><span class="hljs-keyword" style="">type</span></span>
  10. // => <span class="hljs-string" style="">"converse"</span></code>

此方法多适用于GET请求,解析GET里的参数

第三种是 req.params

  1. <code class="hljs cs" style="">官方文档:
  2. An <span class="hljs-keyword" style="">object</span> containing properties mapped to the named route “parameters”.
  3. For example, <span class="hljs-keyword" style="">if</span> you have the route /user/:name,
  4. 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 {}.
  5. 翻译:包含映射到指定的路线“参数”属性的对象。
  6. 例如,如果你有route/user/:name,那么“name”属性可作为req.<span class="hljs-keyword" style="">params</span>.name。
  7. 该对象默认为{}。</code>

nodejs默认提供,无需载入其他中间件

举例说明

  1. <code class="hljs cs" style=""><span class="hljs-comment" style="">// GET /user/tj</span>
  2. req.<span class="hljs-keyword" style="">params</span>.name
  3. <span class="hljs-comment" style="">// => "tj"</span></code>

多适用于restful风格url中的参数的解析

req.query与req.params的区别

req.params包含路由参数(在URL的路径部分),而req.query包含URL的查询参数(在URL的?后的参数)。

最后一种req.param()

此方法被弃用,请看官方解释

  1. <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>.
  2. 翻译:被弃用,用其他三种方式替换</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

最新文章

  1. Flask 重新认识
  2. JSP内置对象---response 响应
  3. php中能够获取到某一网站内容的方法
  4. 常用JVM配置参数
  5. Git简明手册
  6. Atom编辑器
  7. Algorithm --&gt; 快速排序
  8. 蚂蚁通讯框架SOFABolt之私有通讯协议设计
  9. 关于nginx安装的收藏
  10. bzoj4568(合并线性基+倍增)
  11. oracle表空间不足,ORA-00604的解决方法
  12. LeetCode(2): 两数相加
  13. WinPython
  14. .net core 路由处理请求流程图
  15. 去除文件BOM头工具
  16. java的GUI程序的基本思路是以JFrame为基础
  17. UIViewController之间的相互跳转
  18. 《Spring2之站立会议6》
  19. java基础学习:JavaWeb之Cookie和Session
  20. TPO-18 C2 Possible participation in a sociology project

热门文章

  1. Adobe CS2提供免费序列号
  2. C#继承的多态性
  3. javascript入门笔记9-认识DOM
  4. TryParse()的用法
  5. 你可能不知道的 new.target
  6. Liunx 配置sshd服务
  7. xml的应用与dtd约束
  8. .NET领域驱动设计系列(12)
  9. TCL之表达式
  10. ZOJ3553 概率DP