欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-29 12:44:51
express的版本4.x get query参数的获取 url假设:http://localhost:3000/users/zqzjs?name=zhaoq...

express的版本4.x

get

query参数的获取

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

路由的定义:

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)

具体代码:

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

 res.send(_html);
});

结果:

[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

post的数据获取

form data 
 pname:zqz
 pword:job

1.获取需要的query参数

req.body.pname

具体代码:

router.post('/posttest',function(req, res, next){
  var _html = "[post]" +
        "<p><strong>req.body:</strong>"+(json.stringify(req.body) || '')+"</p>" +
        "<p><strong>req.body.name:</strong>"+(req.body.pname || '')+"</p>" +
        "<p><strong>req.body.name:</strong>"+(req.body.pword || '')+"</p>";
  res.send(_html);
})

结果:

[post]
req.body:{"pname":"zqz","pword":"job"}

req.body.name:zqz

req.body.name:job

其他信息

获取请求的头信息

使用:req.header(field)

//http://localhost:3000/users/requestinfo/get
router.get('/requestinfo/get',function(req, res, next){
  var _html = "";
  _html += "[accept] "+req.header('accept')+"<br/>"
  _html += "[accept-encoding] "+req.header('accept-encoding')+"<br/>"
  _html += "[accept-language] "+req.header('accept-language')+"<br/>"
  _html += "[cache-control] "+req.header('cache-control')+"<br/>"
  _html += "[connection] "+req.header('connection')+"<br/>"
  _html += "[cookie] "+req.header('cookie')+"<br/>"
  _html += "[host] "+req.header('host')+"<br/>"
  _html += "[if-none-match] "+req.header('if-none-match')+"<br/>"
  _html += "[upgrade-insecure-requests] "+req.header('upgrade-insecure-requests')+"<br/>"
  _html += "[user-agent] "+req.header('user-agent')+"<br/>"
  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

route

使用:req.route

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

cookies

使用:req.cookies

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

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

hostname

使用:req.hostname

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

//结果:
localhost

ip

使用:req.ip

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

//结果:
::1

originalurl

使用:req.originalurl

//http://localhost:3000/users/originalurl/get
router.get('/originalurl/get',function(req, res, next){
  res.send(req.originalurl);
})

//结果:
/users/originalurl/get

protocol

使用:req.protocol

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

//结果:
http

secure

使用:req.secure

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

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

//结果:
false

xhr

使用:req.xhr

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

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

//结果:
false

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