转自:http://www.cnblogs.com/jkingdom/p/8065202.html
摘要: 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

官方文档解释:
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.
翻译:被弃用,用其他三种方式替换
 

取得 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);
});

67.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. 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. OpenCV问题集锦,图片显示不出来的问题,cvWaitKey(0),不能读图片,未经处理的异常,等问题集合

    昨天根据uc伯克利的人工图像分割文件.seg,显示图像的时候调用了OpenCV的库函数,图片都能用imwrite写好,但是imshow死活显示不出来. 今天早上发现原来是imshow()后面应该加上: ...

  2. ASP.NET Core学习日志1

    1.ASP.NET进行了结构化的优化,使框架更为精简,模块化更加明显. 2.ASP.NET Core不再基于System.Web.dll,而是基于细粒度.分解的NuGet包. 3.基础特性: 1.We ...

  3. vuejs keep-alive

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. SQL--通过身份证号得到年龄的

    /* =======================================创 建 人:CuiYaChao创建日期:2017-08-16功能描述:通过身份证号来计算年龄单元名称: Fun_Ge ...

  5. js中return 、return false 、return true、break、continue区别

    在开发中不熟悉这三者区别的同学,一般都知道return可以中止,但会根据字面意思觉得return true 中止当前函数执行,但其后的函数还会继续执行.return false 中止当前函数执行,其后 ...

  6. 今日SGU 5.29

    sgu 299 题意:给你n个线段,然后问你能不能选出其中三个组成一个三角形,数字很大 收获:另一个大整数模板 那么考虑下为什么如果连续三个不可以的话,一定是不存在呢? 连续上个不合法的话,一定是 a ...

  7. ECNUOJ 2856 仰望星空

    仰望星空 Time Limit:1000MS Memory Limit:65536KBTotal Submit:373 Accepted:145 Description  我仰望星空, 它是那样辽阔而 ...

  8. 转载-- Qt Creator编译时make: arm-linux-g++: command not found 错误!

    前提是已经配置好交叉编译器,但是qt creator找不到. 解决方法: 修改 /usr/local/Trolltech/QtEmbedded-4.7.0-arm/mkspecs/qws/linux- ...

  9. CODEVS——T 2833 奇怪的梦境

    http://codevs.cn/problem/2833/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descr ...

  10. script指定src后内部代码无效

    /********** 无效 ***************/ <script type="text/javascript" src=""> fun ...