www.qjdy.com-奇迹赌场 > www.qjdy.com官网 > name=zhaoqize这样的参数

原标题:name=zhaoqize这样的参数

浏览次数:165 时间:2019-08-22

Express之get,pos央求参数的拿走,expressgetpos乞求

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

如上正是本文的全体内容,希望本文的开始和结果对大家的上学大概干活能推动一定的赞助,同期也可望多多援助帮客之家!

Express的本子4.X Get query参数的得到 url借使: 路由的...

本文由www.qjdy.com-奇迹赌场发布于www.qjdy.com官网,转载请注明出处:name=zhaoqize这样的参数

关键词: mg4355官网

上一篇:本文实例为大家分享了Vuejs实现单文件组件的方法

下一篇:没有了