微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Express之get,pos请求参数的获取

Express的版本4.X

Get

query参数的获取

url假设:http://localhost:3000/users/zqzjs?name=zhaoqize&word=cool&job[st]=web

路由的定义:

rush:js;"> router.get('/:name',function(req,res,next) { })

1.获取?name=zhaoqize这样的参数

req.query.name

2.获取&job[st]=web这样的参数

req.query.job.st

3.获取?name=zhaoqize参数的另外一种写法(4.x版本中该方法已被废弃)

req.param('name')

4.获取所有参数

JSON.stringify(req.query)

具体代码:

rush:js;"> //http://localhost:3000/users/zqzjs?name=zhaoqize&word=cool&job[st]=web //http://localhost:3000/users/zqzjs router.get('/:name',next) { //4.版本中已被废弃,不建议使用 req.param('name') console.log(JSON.stringify(req.query) || '') //获取get请求参数 var _html = "Params"+ "

req.params:

"+(JSON.stringify(req.params) || '')+"

" + "

req.params.name:

"+(req.params.name || '')+"

" + "Query:" + "

req.query:

"+(JSON.stringify(req.query) || '')+"

" + "

req.query.name:

"+(req.query.name || '')+"

" + "

req.query.word:

"+(req.query.word || '')+"

"+ "

req.query.job.st:

"+(req.query.job.st || '')+"

" + "

req.param('name'):

"+(req.param('name') || '')+"

" ;

res.send(_html);
});

结果:

rush:js;"> [Get] Params req.params:{"name":"zqzjs"}

req.params.name:zqzjs

Query:

req.query:{"name":"zhaoqize","word":"cool","job":{"st":"web"}}

req.query.name:zhaoqize

req.query.word:cool

req.query.job.st:web

req.param('name'):zqzjs

Post

获取

rush:js;"> Form Data pName:zqz pWord:job

1.获取需要的query参数

req.body.pName

具体代码:

rush:js;"> router.post('/postTest',next){ var _html = "[Post]" + "

req.body:

"+(JSON.stringify(req.body) || '')+"

" + "

req.body.name:

"+(req.body.pName || '')+"

" + "

req.body.name:

"+(req.body.pWord || '')+"

"; res.send(_html); })

结果:

rush:js;"> [Post] req.body:{"pName":"zqz","pWord":"job"}

req.body.name:zqz

req.body.name:job

其他信息

获取请求的头信息

使用:req.header(field)

rush:js;"> //http://localhost:3000/users/requestInfo/get router.get('/requestInfo/get',next){ var _html = ""; _html += "[Accept] "+req.header('Accept')+"
" _html += "[Accept-Encoding] "+req.header('Accept-Encoding')+"
" _html += "[Accept-Language] "+req.header('Accept-Language')+"
" _html += "[Cache-Control] "+req.header('Cache-Control')+"
" _html += "[Connection] "+req.header('Connection')+"
" _html += "[Cookie] "+req.header('Cookie')+"
" _html += "[Host] "+req.header('Host')+"
" _html += "[if-none-match] "+req.header('if-none-match')+"
" _html += "[Upgrade-Insecure-Requests] "+req.header('Upgrade-Insecure-Requests')+"
" _html += "[User-Agent] "+req.header('User-Agent')+"
" res.send(_html); })

//结果:
[Accept] text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
[Accept-Encoding] gzip,deflate,sdch,br
[Accept-Language] zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4,ja;q=0.2
[Cache-Control] max-age=0
[Connection] keep-alive
[Cookie] Hm_lvt_4f16d955a2236fc6ca7287644ecc9d79=1480576847,1481166685
[Host] localhost:3000
[if-none-match] W/"23b-OjTNcZfCLTvw659CYGlLrA"
[Upgrade-Insecure-Requests] 1
[User-Agent] Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/54.0.2840.99 Safari/537.36

使用:req.route

rush:css;"> //http://localhost:3000/users/router/get router.get('/router/get',next){ res.send(req.route); }) //结果: { path: "/router/get",stack: [ { name: "",keys: [ ],regexp: { },method: "get" } ],methods: { get: true } }

cookies

使用:req.cookies

rush:js;"> //http://localhost:3000/users/cookies/get router.get('/cookies/get',next){ res.send(req.cookies); })

//结果:
{
Hm_lvt_4f16d955a2236fc6ca7287644ecc9d79: "1480576847,1481166685"
}

hostname

使用:req.hostname

rush:js;"> //http://localhost:3000/users/hostname/get router.get('/hostname/get',next){ res.send(req.hostname); })

//结果:
localhost

ip

使用:req.ip

rush:js;"> //http://localhost:3000/users/ip/get router.get('/ip/get',next){ res.send(req.ip); })

//结果:
::1

originalUrl

使用:req.originalUrl

rush:js;"> //http://localhost:3000/users/originalUrl/get router.get('/originalUrl/get',next){ res.send(req.originalUrl); })

//结果:
/users/originalUrl/get

protocol

使用:req.protocol

rush:js;"> //http://localhost:3000/users/protocol/get router.get('/protocol/get',next){ res.send(req.protocol); })

//结果:
http

secure

使用:req.secure

说明:用来判断协议是否安全,如果是https,返回的就是true

rush:js;"> //http://localhost:3000/users/secure/get router.get('/secure/get',next){ res.send(req.secure); })

//结果:
false

xhr

使用:req.xhr

说明:判断是否是异步请求

rush:java;"> //http://localhost:3000/users/xhr/get router.get('/xhr/get',next){ res.send(req.xhr); })

//结果:
false

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持编程之家!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐