国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Express之get,pos請求參數(shù)的獲取

2019-11-19 16:40:43
字體:
供稿:網(wǎng)友

Express的版本4.X

Get

query參數(shù)的獲取

url假設(shè):http://localhost:3000/users/zqzjs?name=zhaoqize&word=cool&job[st]=web

路由的定義:

router.get('/:name', function(req, res, next) {})

1.獲取?name=zhaoqize這樣的參數(shù)

req.query.name

2.獲取&job[st]=web這樣的參數(shù)

req.query.job.st

3.獲取?name=zhaoqize參數(shù)的另外一種寫法(4.x版本中該方法已被廢棄)

req.param('name')

4.獲取所有參數(shù)

JSON.stringify(req.query)

具體代碼:

//http://localhost:3000/users/zqzjs?name=zhaoqize&word=cool&job[st]=web//http://localhost:3000/users/zqzjsrouter.get('/:name', function(req, res, next) { //4.版本中已被廢棄,不建議使用 req.param('name') console.log(JSON.stringify(req.query) || '') //獲取get請求參數(shù) 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);});

結(jié)果:

[Get]Paramsreq.params:{"name":"zqzjs"}req.params.name:zqzjsQuery:req.query:{"name":"zhaoqize","word":"cool","job":{"st":"web"}}req.query.name:zhaoqizereq.query.word:coolreq.query.job.st:webreq.param('name'):zqzjs

Post

post的數(shù)據(jù)獲取

Form Data  pName:zqz pWord:job

1.獲取需要的query參數(shù)

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);})

結(jié)果:

[Post]req.body:{"pName":"zqz","pWord":"job"}req.body.name:zqzreq.body.name:job

其他信息

獲取請求的頭信息

使用:req.header(field)

//http://localhost:3000/users/requestInfo/getrouter.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);})//結(jié)果:[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/getrouter.get('/router/get',function(req, res, next){  res.send(req.route);})//結(jié)果:{path: "/router/get",stack: [{name: "<anonymous>",keys: [ ],regexp: { },method: "get"}],methods: {get: true}}

cookies

使用:req.cookies

//http://localhost:3000/users/cookies/getrouter.get('/cookies/get',function(req, res, next){  res.send(req.cookies);})//結(jié)果:{Hm_lvt_4f16d955a2236fc6ca7287644ecc9d79: "1480576847,1481166685"}

hostname

使用:req.hostname

//http://localhost:3000/users/hostname/getrouter.get('/hostname/get',function(req, res, next){  res.send(req.hostname);})//結(jié)果:localhost

ip

使用:req.ip

//http://localhost:3000/users/ip/getrouter.get('/ip/get',function(req, res, next){  res.send(req.ip);})//結(jié)果:::1

originalUrl

使用:req.originalUrl

//http://localhost:3000/users/originalUrl/getrouter.get('/originalUrl/get',function(req, res, next){  res.send(req.originalUrl);})//結(jié)果:/users/originalUrl/get

protocol

使用:req.protocol

//http://localhost:3000/users/protocol/getrouter.get('/protocol/get',function(req, res, next){  res.send(req.protocol);})//結(jié)果:http

secure

使用:req.secure

說明:用來判斷協(xié)議是否安全,如果是https,返回的就是true

//http://localhost:3000/users/secure/getrouter.get('/secure/get',function(req, res, next){  res.send(req.secure);})//結(jié)果:false

xhr

使用:req.xhr

說明:判斷是否是異步請求

//http://localhost:3000/users/xhr/getrouter.get('/xhr/get',function(req, res, next){  res.send(req.xhr);})//結(jié)果:false

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持武林網(wǎng)!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 大理市| 安康市| 垦利县| 鄄城县| 连江县| 二连浩特市| 延长县| 福建省| 柳林县| 佛教| 凤城市| 孟州市| 嘉禾县| 石河子市| 南投市| 新竹市| 新和县| 济源市| 金堂县| 岳西县| 佛教| 栾城县| 曲水县| 朝阳县| 揭阳市| 安图县| 高州市| 衡水市| 晋中市| 聊城市| 天等县| 泾源县| 灵台县| 晋宁县| 蓬安县| 巩留县| 宁武县| 屯留县| 邯郸市| 六枝特区| 星座|