转自: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);
});

最新文章

  1. Drupal7网站+IIS7.0+PHP+MySql
  2. SOAPFaultException
  3. oracle row_number()
  4. POJ 2983 Is the Information Reliable? 信息可靠吗 (差分约束,spfa)
  5. 学习马士兵的struts2/hibernate/spring中遇到的问题及其解决方法
  6. Mod_Python中文文档
  7. Spring Cloud 前后端分离后引起的跨域访问解决方案
  8. Android Studio 插件开发详解四:填坑
  9. 【Spark篇】---SparkStreaming算子操作transform和updateStateByKey
  10. 10.3 Vue 路由系统
  11. okvis代码解读
  12. CrazySNS has on line - And you&#39;ll see why 1984 won&#39;t be like &quot;1984.&quot;
  13. Working With JSON
  14. Ubuntu 安装IntelliJ IDEA
  15. GIT(6)----fork和clone的区别,fetch与pull的区别
  16. Android自己定义组件系列【3】——自己定义ViewGroup实现側滑
  17. C++经典排序算法总结
  18. 日期时间函数(1)-time()&amp;gmtime()&amp;strftime()&amp;localtime()
  19. erlang supervisor中启动普通的进程
  20. C++中内部类访问外部类的私有成员

热门文章

  1. 洛谷P3195 [HNOI2008]玩具装箱TOY(单调队列优化DP)
  2. PostgreSQL Replication之第二章 理解PostgreSQL的事务日志(1)
  3. javascript--记忆函数
  4. MySql系列之多表查询
  5. vsCode 快捷键、插件
  6. ifreq、ifconf
  7. ArcGIS api for javascript——图层-创建定制的切片图层类型的图层
  8. vue12 循环添加重复数据
  9. linux log日志解析
  10. OpenCV中InputArray和OutputArray使用方法