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 ...
随机推荐
- leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- Android——PullToRefresh自动刷新
需求:强制刷新 方法一: PullToRefreshListView本身提供了一个setRefreshing()接口,调用该接口会自动触发下拉刷新的操作(前提是支持下拉刷新).按照一般的操作我们直接在 ...
- Input Team
The Chromium Input team (aka input-dev) is a web platform team focused on making touch (P1) and othe ...
- 大数问题(相加) A + B
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum o ...
- JAVA-截取字符串两边指定字符
工具类: /** * 工具类 */ public class Tool { /** * 截取两边指定的字符 * @param character * @param symbol * @return * ...
- [洛谷P3929]SAC E#1 - 一道神题 Sequence1
题目大意:给你一串数列,问你能否改变1个数或不改,使它变成波动数列? 一个长度为n的波动数列满足对于任何i(1 <= i < n),均有: a[2i-1] <= a[2i] 且 a[ ...
- uptime---系统总共运行时间和系统的平均负载
uptime命令能够打印系统总共运行了多长时间和系统的平均负载.uptime命令可以显示的信息显示依次为:现在时间.系统已经运行了多长时间.目前有多少登陆用户.系统在过去的1分钟.5分钟和15分钟内的 ...
- Atitit.软件开发的终于的设计 dsl化,ast化(建立ast, 解析运行ast)
Atitit.软件开发的终于的设计 dsl化,ast化(建立ast, 解析运行ast) 1. 使用js,html 撰写dsl 1 1.1. 架构图 1 1.2. html 2 1.3. Js 2 1. ...
- [B cannot be cast to java.lang.String
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.inv ...
- js最近天数
//七天查询 recent(6); //30天查询 recent(30); //最近天数 var recent=function(arg){ var myDate = new Date(); //获取 ...