摘要: 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

nodejs取参四种方法req.body,req.params,req.param,req.body的更多相关文章

  1. 67.nodejs取参四种方法req.body,req.params,req.param,req.body

    转自:http://www.cnblogs.com/jkingdom/p/8065202.html 摘要: nodejs取参四种方法req.body,req.params,req.param,req. ...

  2. nodejs取参四种方法 req.body, req.params, req.param, req.body

    获取请求很中的参数是每个web后台处理的必经之路,nodejs的 express框架 提供了四种方法来实现. req.body req.query req.params req.param() 首先介 ...

  3. node.js取参四种方法req.body,req.params,req.param,req.body

    参考:https://my.oschina.net/u/2519530/blog/535309 获取请求很中的参数是每个web后台处理的必经之路,nodejs的 express框架 提供了四种方法来实 ...

  4. nodeJS学习(11)--- nodeJS 取参 -- req.body & req.query & req.params

    参考:https://my.oschina.net/u/2519530/blog/535309 获取请求中的参数是每个web后台处理的必经之路,nodejs的 express框架 提供了四种方法来实现 ...

  5. Java中取小数点后两位(四种方法)

    摘自http://irobot.iteye.com/blog/285537 Java中取小数点后两位(四种方法)   一 Long是长整型,怎么有小数,是double吧     java.text.D ...

  6. Angular--页面间切换及传值的四种方法

    1. 基于ui-router的页面跳转传参(1) 在AngularJS的app.js中用ui-router定义路由,比如现在有两个页面,一个页面(producers.html)放置了多个produce ...

  7. IOS中Json解析的四种方法

    作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验 ...

  8. (转载)eclipse插件安装的四种方法

    eclipse插件安装的四种方法 Eclipse插件的安装方法 1.在eclipse的主目录(ECLIPSE_HOME, 比如在我的机器上安装的目录是:D:\eclipse)有一个plugins的目录 ...

  9. 转载:遍历Map的四种方法

    http://www.cnblogs.com/kristain/articles/2033566.html 遍历Map的四种方法 public static void main(String[] ar ...

随机推荐

  1. 如何让HttpWebRequest使用指定网络接口传输数据

    using System; using System.Net; class Program { public static void Main () { foreach (var ip in Dns. ...

  2. (python)剑指Offer:数组中重复的数字

    问题描述 在长度为n的数组中,所有的元素都是0到n-1的范围内. 数组中的某些数字是重复的,但不知道有几个重复的数字,也不知道重复了几次,请找出任意重复的数字. 例如,输入长度为7的数组{2,3,1, ...

  3. EF core 学习笔记

    应该 以领域 为核心开发程序, 不应该 以数据库 entityframeworkcore entityframeworkcore.sqlserver entityframeworkcore.tool ...

  4. redux详解

    redux介绍 学习文档:英文文档,中文文档,Github redux是什么 redux是一个独立专门用于做状态管理的JS库(不是react插件库),它可以用在react, angular, vue等 ...

  5. BZOJ1965: [Ahoi2005]SHUFFLE 洗牌(exgcd 找规律)

    Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 989  Solved: 660[Submit][Status][Discuss] Description ...

  6. ajax状态值和状态码

    AJAX状态值是指,运行AJAX所经历过的几种状态,无论访问是否成功都将响应的步骤,可以理解成为AJAX运行步骤.如:正在发送,正在响应等,由AJAX对象与服务器交互时所得:使用“ajax.ready ...

  7. python__基础 : 类的__new__方法与实现一个单例

    __new__ : 这个方法的作用主要是创建一个实例,在创建实例时首先会调用 __new__方法 ,然后调用__init__对实例进行初始化, 如果想修改 __new__ 这个方法,那么最后要 ret ...

  8. 【PHP项目】form表单的enctype属性

    enctype这个属性管理的是表单的MIME(Multipurpose Internet Mail Extensions)编码,共有三个值可选: 1.application/x-www-form-ur ...

  9. 大话目标检测经典模型(RCNN、Fast RCNN、Faster RCNN)

      目标检测是深度学习的一个重要应用,就是在图片中要将里面的物体识别出来,并标出物体的位置,一般需要经过两个步骤:1.分类,识别物体是什么 2.定位,找出物体在哪里 除了对单个物体进行检测,还要能支持 ...

  10. 9 10mins的投票功能

    1.投票的原理 2.投票的数据结构设计 (1)准备工作 导入detail页面 配置静态文件 <link rel="stylesheet" href="../stat ...