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

  1. req.body

  2. req.query

  3. req.params

  4. req.param()

首先介绍第一个req.body

官方文档解释:
Contains key-value pairs of data submitted in the request body. By default, it is undefined,
and is populated when you use body-parsing middleware such as body-parser and multer. 稍微翻译一下:包含了提交数据的键值对在请求的body中,默认是underfined,
你可以用body-parser或者multer来解析body

解析body不是nodejs默认提供的,你需要载入body-parser中间件才可以使用req.body

此方法通常用来解析POST请求中的数据

第二种是req.query

官方文档解释:
An object containing a property for each query string parameter in the route.
If there is no query string, it is the empty object, {}.
翻译一下:包含在路由中每个查询字符串参数属性的对象。如果没有,默认为{}

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

举例说明(官方摘抄):

// 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"

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

第三种是 req.params

官方文档:
An object containing properties mapped to the named route “parameters”.
For example, if you have the route /user/:name,
then the “name” property is available as req.params.name. This object defaults to {}. 翻译:包含映射到指定的路线“参数”属性的对象。
例如,如果你有route/user/:name,那么“name”属性可作为req.params.name。
该对象默认为{}。

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

举例说明

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

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

req.query与req.params的区别

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

最后一种req.param()

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

Deprecated. Use either req.params, req.body or req.query, as applicable.
翻译:被弃用,用其他三种方式替换

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

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

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

  2. 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. ...

  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. rabbitMQ centos7 的安装

    安装erlang 1:下载erlang. http://erlang.org/download/otp_src_20.3.tar.gz 2:把erlang压缩包上传到Linux服务器上,并解压.我的解 ...

  2. k8s中的网络(较详细汇总)

    目录 一.网络前提条件-网络模型 二.需要解决的网络问题 1.容器和容器之间的网络 2.pod与pod之间的网络 同一台node节点上pod和pod通信 不同node节点上pod和pod通信 3.po ...

  3. 算法笔试过程中的几个输入输出python语句

    title: python在线笔试学习笔记 localimage: image1 urlname: writenexam categories: summary tags: [writen, exam ...

  4. TIME_WAIT状态全是3306解决办法

    刚吃完晚饭,手机短信一直响个不停,打开一看全是告警信息,立即打开电脑查看,发现很多网页很不稳定  一会能打开,一会打不开 登录服务器查看负载情况,cpu.内存 .磁盘io 负载都不高,查看日志发现ng ...

  5. linux修改MAC的方法

    Linux修改MAC地址方法 Linux modifies MAC address method 1 ifconfig wlan0 down 2 ifconfig wlan0 hw ether MAC ...

  6. jquery基础知识2

    1.js和jquery对象的转换 js==>jquery对象 $(js对象) jquery==>js jq对象[index] jq对象.get(index) <!DOCTYPE ht ...

  7. 初识python中的68个内置函数

    内置函数思维导图的链接: https://www.processon.com/view/link/5b72b805e4b08d3622ad8b48 面向对象和反射相关的后面补充

  8. SpringBoot项目的异常

    1.问题描述 创建springcloud 项目时候,main报错: Spring Boot Application in default package less… (Ctrl+F1)Inspecti ...

  9. 0020SpringBoot使用SpringCloud中的eureka实现远程调用

    要实现远程调用,主要需要三个module:一个注册中心.一个服务提供者.一个服务消费者,然后进行各自的配置和编码,详细内容如下: 1.建一个空的project,创建3各module a.注册中心模块 ...

  10. 二、操作XML DOM:XML Document

    需要添加的命名空间:using System.Xml; 一.创建xml文件: 1.XmlDocument方式创建 XmlDocument xmldoc = new XmlDocument(); //加 ...