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

摘要: 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

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

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

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

第二种是req.query

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

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

举例说明(官方摘抄):

  1.  
    // GET /search?q=tobi+ferret
  2.  
    req.query.q
  3.  
    // => "tobi ferret"
  4.  
     
  5.  
    // GET /shoes?order=desc&shoe[color]=blue&shoe[type]=converse
  6.  
    req.query.order
  7.  
    // => "desc"
  8.  
    req.query.shoe.color
  9.  
    // => "blue"
  10.  
    req.query.shoe.type
  11.  
    // => "converse"

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

第三种是 req.params

 

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

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

举例说明

  1.  
    // GET /user/tj
  2.  
    req.params.name
  3.  
    // => "tj"

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

req.query与req.params的区别

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

最后一种req.param()

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

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

 

 

 

取得 GET Request 的 Query Strings:

  1.  
    GET /test?name=fred&tel=0926xxx572
  2.  
     
  3.  
    app.get('/test', function(req, res) {
  4.  
    console.log(req.query.name);
  5.  
    console.log(req.query.tel);
  6.  
    });

如果是表单且是用 POST method:

  1.  
    <form action='/test' method='post'>
  2.  
    <input type='text' name='name' value='fred'>
  3.  
    <input type='text' name='tel' value='0926xxx572'>
  4.  
    <input type='submit' value='Submit'>
  5.  
    </form>
  6.  
    app.post('/test', function(req, res) {
  7.  
    console.log(req.query.id);
  8.  
    console.log(req.body.name);
  9.  
    console.log(req.body.tel);
  10.  
    });

当然也可以 Query Strings 和 POST method 的表单同时使用:

  1.  
    <form action='/test?id=3' method='post'>
  2.  
    <input type='text' name='name' value='fred'>
  3.  
    <input type='text' name='tel' value='0926xxx572'>
  4.  
    <input type='submit' value='Submit'>
  5.  
    </form>
  6.  
    app.post('/test', function(req, res) {
  7.  
    console.log(req.query.id);
  8.  
    console.log(req.body.name);
  9.  
    console.log(req.body.tel);
  10.  
    });

顺带补充,还有另一种方法传递参数给 Server,就是使用路径的方式,可以利用 Web Server 的 HTTP Routing 來解析,常见于各种 Web Framework。這不算是传统标准规范的做法,是属于 HTTP Routing 的延伸应用。

  1.  
    GET /hello/fred/0926xxx572
  2.  
     
  3.  
    app.get('/hello/:name/:tel', function(req, res) {
  4.  
    console.log(req.params.name);
  5.  
    console.log(req.params.tel);
  6.  
    });

来源:http://liuxufei.com/blog/jishu/798.html

params和data的区别,即get和post请求的区别,HTTP协议的基础知识

get请求参数是带在url上的,必须使用params

post请求是body data使用data

假设url:http://192.168.1.111:8080/api/cluster/group?wzd=111&abc=cc
方法类型:POST,body是{"name":"abc"}
 
1. request.query
得到一个bottle的FormsDict对象,该对象可以转化为字典,里面的内容是:
{"wzd":"111","abc":"cc"},即,是url中后面的参数
 
2.request.params
也是得到FormsDict对象,转化为字典后,其内容是:
{"wzd":"111","abc":"cc","{"name":"abc"}":""}
即,其内容包含了url后的参数和值,同时也包含了body中的值,要注意的是,它把body中所以的参数作为一个key存入了。
 
3.request.body
返回一个StringIO对象,通过read方法取出的数据是body里的所有值,不管body里是不是json该方法都原样返回body里的所有内容。对本例而言是返回:{"name":"abc"}
 
4.request.query_string
它得到的是,url中?后面所有的值,最为一个字符串,即:wzd=111&abc=cc
 
5.request.json
当请求的Content-Type`` 是`application/json的时候,该方法返回的是body中的json串,如果body中不是json会抛出异常:ValueError: No JSON object could be decoded,对应本例,返回:{"name":"abc"}
 
6.request.form
有这么一个表单:
1
2
3
4
5
<form action="/login" method="post">
Username: <input name="username1" type="text" />
Password: <input name="password1" type="password" />
<input value="Login" type="submit">
</form>
那么要获取username/password有如下方法:
方法一:
username = request.forms.get('username1') # 对应的是Username输入框中的name属性
password = request.forms.get('password1') # 对应的是password输入框中的name属性
方法二:
username = request.POST.get('username')
password = request.POST.get('password')
并且,上面两种get方法都可以跟一个默认值,当username或者password不存在的时候返回设置的默认值,如:username = request.POST.get('username','abc'),当username不存在的时候,返回abc,如果不设置,返回none
 
另外,bottle.request.forms,返回表单中所有的k,v,即:{“username”:"123","password":"324"}

body:请求体中的数据

query:请求的参数,URL后面以?的形式,例:user?id

params:请求的参数,URL后面以/的形式,例:user/:id

posted @ 2019-11-25 20:39  ABDM  阅读(3664)  评论(0编辑  收藏  举报