67.nodejs取参四种方法req.body,req.params,req.param,req.body
获取请求很中的参数是每个web后台处理的必经之路,nodejs的 express框架 提供了四种方法来实现。
req.body
req.query
req.params
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的更多相关文章
- nodejs取参四种方法req.body,req.params,req.param,req.body
摘要: nodejs取参四种方法req.body,req.params,req.param,req.body 获取请求很中的参数是每个web后台处理的必经之路,nodejs提供了四种方法来实现. 获取 ...
- nodejs取参四种方法 req.body, req.params, req.param, req.body
获取请求很中的参数是每个web后台处理的必经之路,nodejs的 express框架 提供了四种方法来实现. req.body req.query req.params req.param() 首先介 ...
- node.js取参四种方法req.body,req.params,req.param,req.body
参考:https://my.oschina.net/u/2519530/blog/535309 获取请求很中的参数是每个web后台处理的必经之路,nodejs的 express框架 提供了四种方法来实 ...
- nodeJS学习(11)--- nodeJS 取参 -- req.body & req.query & req.params
参考:https://my.oschina.net/u/2519530/blog/535309 获取请求中的参数是每个web后台处理的必经之路,nodejs的 express框架 提供了四种方法来实现 ...
- Java中取小数点后两位(四种方法)
摘自http://irobot.iteye.com/blog/285537 Java中取小数点后两位(四种方法) 一 Long是长整型,怎么有小数,是double吧 java.text.D ...
- Angular--页面间切换及传值的四种方法
1. 基于ui-router的页面跳转传参(1) 在AngularJS的app.js中用ui-router定义路由,比如现在有两个页面,一个页面(producers.html)放置了多个produce ...
- IOS中Json解析的四种方法
作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验 ...
- (转载)eclipse插件安装的四种方法
eclipse插件安装的四种方法 Eclipse插件的安装方法 1.在eclipse的主目录(ECLIPSE_HOME, 比如在我的机器上安装的目录是:D:\eclipse)有一个plugins的目录 ...
- 转载:遍历Map的四种方法
http://www.cnblogs.com/kristain/articles/2033566.html 遍历Map的四种方法 public static void main(String[] ar ...
随机推荐
- 状态压缩dp初学__$Corn Fields$
明天计划上是要刷状压,但是作为现在还不会状压的\(ruoruo\)来说是一件非常苦逼的事情,所以提前学了一下状压\(dp\). 鸣谢\(hmq\ juju\)的友情帮助 状态压缩动态规划 本博文的大体 ...
- ip反查域名的详细信息(多种方法)
不多说,直接上干货! 至于这里怎FQ,很简单,请见我下面的博客! kali 2.0安装 lantern(成功FQ) shadowsocks(简称SSFQ软件)步骤详解 FQ软件lantern-inst ...
- Hadoop框架基础(二)
** Hadoop框架基础(二) 上一节我们讨论了如何对hadoop进行基础配置已经运行一个简单的实例,接下来我们尝试使用eclipse开发. ** maven安装 简单介绍:maven是一个项目管理 ...
- 文件IO流总结
文件在网络上或不同设备之间是怎么传输的,在Java程序中又是怎么来实现文件的传输,带着这两个问题,来了解一下Java中的IO流相关类及操作. 一.什么是流及流的用途 流是一组有顺序,有起点和终点的字节 ...
- Mojo C++ Platform API
Mojo C++ Platform API This document is a subset of the Mojo documentation. Contents Overview Platfor ...
- 20180929 北京大学 人工智能实践:Tensorflow笔记06
入戏 需要修改成如下: (完)
- 一 梳理 从 HDFS 到 MR。
MapReduce 不仅仅是一个工具,更是一个框架.我们必须拿问题解决方案去适配框架的 map 和 reduce 过程 很多情况下,需要关注 MapReduce 作业所需要的系统资源,尤其是集 ...
- postgresql拓展if、ifnull、group_concat函数
postgresql版本是8.2.15. 最近陆续有数据分析师从impala.hive转到查询gpdb,gpdb虽然能够支持在查询语句中带多个distinct,但是缺少相应的if.ifnull.gro ...
- KM最大匹配 HDU 2255
KM算法详解+模板 - wenr - 博客园 http://www.cnblogs.com/wenruo/p/5264235.html #include<iostream> #inclu ...
- BZOJ3158: 千钧一发
[传送门:BZOJ3158] 简要题意: 给出n个机器,每个机器有a[i]基础值和b[i]价值 选出一部分机器使得这些机器里面两两至少满足以下两种条件之一: 1.a[i]2+a[j]2!=T2(T为正 ...