Express之get,pos請(qǐng)求參數(shù)的獲取
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í)也希望多多支持腳本之家!
- 詳解Retrofit 動(dòng)態(tài)參數(shù)(非固定參數(shù)、非必須參數(shù))(Get、Post請(qǐng)求)
- request請(qǐng)求獲取參數(shù)的實(shí)現(xiàn)方法(post和get兩種方式)
- 頁(yè)面get請(qǐng)求 中文參數(shù)方法亂碼問(wèn)題的快速解決方法
- Java如何實(shí)現(xiàn)URL帶請(qǐng)求參數(shù)(get/post)及得到get和post請(qǐng)求url和參數(shù)列表的方法
- 動(dòng)態(tài)加載iframe時(shí)get請(qǐng)求傳遞中文參數(shù)亂碼解決方法
- js筆試題-接收get請(qǐng)求參數(shù)
相關(guān)文章
windows使用nvm對(duì)node進(jìn)行版本管理切換的完整步驟
這篇文章主要介紹了windows使用nvm對(duì)node進(jìn)行版本管理切換的完整步驟,在使用之前各位務(wù)必卸載掉自己安裝過(guò)的nvm或者node版本包括環(huán)境變量之類(lèi)的,要保證自己的電腦完全沒(méi)有node環(huán)境,需要的朋友可以參考下2024-03-03
基于Alpine Linux構(gòu)建前端node-web鏡像步驟詳解
這篇文章主要為大家介紹了基于Alpine Linux構(gòu)建前端node-web鏡像步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
nodejs實(shí)現(xiàn)截取上傳視頻中一幀作為預(yù)覽圖片
這篇文章主要為大家詳細(xì)介紹了nodejs實(shí)現(xiàn)截取上傳視頻中一幀作為預(yù)覽圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Nodejs對(duì)postgresql基本操作的封裝方法
今天小編就為大家分享一篇Nodejs對(duì)postgresql基本操作的封裝方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Node.js開(kāi)源應(yīng)用框架HapiJS介紹
這篇文章主要介紹了Node.js開(kāi)源應(yīng)用框架HapiJS介紹,本文講解了HapiJS介紹、HapiJS安裝和項(xiàng)目配置和開(kāi)發(fā)實(shí)例等內(nèi)容,需要的朋友可以參考下2015-01-01
nodejs檢測(cè)因特網(wǎng)是否斷開(kāi)的解決方案
這篇文章主要給大家介紹了關(guān)于nodejs如何檢測(cè)因特網(wǎng)是否斷開(kāi)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用nodejs具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
從零學(xué)習(xí)node.js之搭建http服務(wù)器(二)
NodeJs是什么我想大家已經(jīng)很了解了,作為一個(gè)前端工程師,想要不斷提升自己,一般都會(huì)接觸到后端服務(wù)器的技術(shù),NodeJs給我們提供了一個(gè)學(xué)習(xí)成本相對(duì)較低的捷徑可走。下面這篇文章主要介紹了node.js中搭建http服務(wù)器的相關(guān)資料,需要的朋友可以參考下。2017-02-02

