欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年05月02日 17:11:36   作者:leslie·Zhao  
本篇文章主要介紹了Express之get,pos請(qǐng)求參數(shù)的獲取的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧

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ù)的另外一種寫(xiě)法(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/zqzjs
router.get('/:name', function(req, res, next) {
 //4.版本中已被廢棄,不建議使用
 req.param('name')
 console.log(JSON.stringify(req.query) || '')
 //獲取get請(qǐng)求參數(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]
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的數(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:zqz

req.body.name:job

其他信息

獲取請(qǐng)求的頭信息

使用: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);
})

//結(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/get
router.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/get
router.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/get
router.get('/hostname/get',function(req, res, next){
  res.send(req.hostname);
})

//結(jié)果:
localhost

ip

使用:req.ip

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

//結(jié)果:
::1

originalUrl

使用:req.originalUrl

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

//結(jié)果:
/users/originalUrl/get

protocol

使用:req.protocol

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

//結(jié)果:
http

secure

使用:req.secure

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

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

//結(jié)果:
false

xhr

使用:req.xhr

說(shuō)明:判斷是否是異步請(qǐng)求

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

//結(jié)果:
false

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

  • IDEA中配置node.js的實(shí)現(xiàn)步驟

    IDEA中配置node.js的實(shí)現(xiàn)步驟

    在IDEA中,如果要在安裝在遠(yuǎn)程主機(jī)或虛擬環(huán)境中的Node.js上運(yùn)行和調(diào)試應(yīng)用程序,則需要配置遠(yuǎn)程N(yùn)ode.js解釋器,本文主要介紹了IDEA中配置node.js的實(shí)現(xiàn)步驟,感興趣的可以了解一下
    2023-12-12
  • npm安裝淘寶鏡像報(bào)錯(cuò)問(wèn)題解決(npm install -g cnpm)

    npm安裝淘寶鏡像報(bào)錯(cuò)問(wèn)題解決(npm install -g cnpm)

    本文主要介紹了npm安裝淘寶鏡像報(bào)錯(cuò)問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • NodeJs模擬登陸正方教務(wù)

    NodeJs模擬登陸正方教務(wù)

    網(wǎng)上已經(jīng)有很多關(guān)于模擬登陸正方教務(wù)的作品了,基于 PHP,Python,Java,.Net 加上NodeJs,這幾門(mén)語(yǔ)言都可以實(shí)現(xiàn)模擬登陸,模擬登陸的技術(shù)點(diǎn)不是特別難,這里記錄一下利用Node碰到的一些坑,以及一些解決思路。
    2017-04-04
  • node.js中的fs.rmdirSync方法使用說(shuō)明

    node.js中的fs.rmdirSync方法使用說(shuō)明

    這篇文章主要介紹了node.js中的fs.rmdirSync方法使用說(shuō)明,本文介紹了fs.rmdirSync方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下
    2014-12-12
  • Node.js搭建WEB服務(wù)器的示例代碼

    Node.js搭建WEB服務(wù)器的示例代碼

    這篇文章主要介紹了Node.js搭建WEB服務(wù)器的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • 關(guān)于Node.js的events.EventEmitter用法介紹

    關(guān)于Node.js的events.EventEmitter用法介紹

    本篇文章主要介紹了關(guān)于Node.js的events.EventEmitter用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-04-04
  • 使用node.JS中的url模塊解析URL信息

    使用node.JS中的url模塊解析URL信息

    本文將詳細(xì)介紹nodeJS中的URL模塊的使用方法,利用URL模塊解析出URL相關(guān)信息
    2020-02-02
  • node.js使用http模塊創(chuàng)建服務(wù)器和客戶端完整示例

    node.js使用http模塊創(chuàng)建服務(wù)器和客戶端完整示例

    這篇文章主要介紹了node.js使用http模塊創(chuàng)建服務(wù)器和客戶端,結(jié)合完整示例形式分析了node.js基于http模塊實(shí)現(xiàn)客戶端與服務(wù)器端交互的相關(guān)操作技巧,需要的朋友可以參考下
    2020-02-02
  • node.js中格式化數(shù)字增加千位符的幾種方法

    node.js中格式化數(shù)字增加千位符的幾種方法

    這篇文章主要介紹了node.js中格式化數(shù)字增加千位符的幾種方法,本文給出3種實(shí)現(xiàn)方法,并分別給出實(shí)例代碼,需要的朋友可以參考下
    2015-07-07
  • windows 下安裝nodejs 環(huán)境變量設(shè)置

    windows 下安裝nodejs 環(huán)境變量設(shè)置

    windows 下安裝nodejs 了,也安裝了npm, 但是有時(shí)候切不能直接用request(‘ws’)這一類(lèi)的東西.我覺(jué)得是確實(shí)環(huán)境變量或其他設(shè)置有問(wèn)題,能否給個(gè)完整的設(shè)置方案:
    2017-02-02

最新評(píng)論