开发后怎么使用小程序_Express之get,pos请求参数的

发布时间:2021-01-12 11:38 作者:jianzhan

摘要: Express之get,pos恳求主要参数的获得 ·Zhao query主要参数的获得url假定:users/zqzjs name=zhaoqize word=cool job[st]=web路由器的界定:router.get('/:name', function(req, res,

Express之get,pos请求参数的获取     ·Zhao   本篇文章主要介绍了Express之get,pos请求参数的获取的相关知识,具有很好的参考价值。下面跟着小编一起来看下吧

query参数的获取

url假设: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)

具体代码:

//users/zqzjs name=zhaoqize word=cool job[st]=web
//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 

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)

//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] 9d79=
[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

//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

//users/cookies/get
router.get('/cookies/get',function(req, res, next){
 res.send(req.cookies);
//结果:
9d79: ""

hostname

使用:req.hostname

//users/hostname/get
router.get('/hostname/get',function(req, res, next){
 res.send(req.hostname);
//结果:
localhost

ip

使用:req.ip

//users/ip/get
router.get('/ip/get',function(req, res, next){
 res.send(req.ip);
//结果:

originalUrl

使用:req.originalUrl

//users/originalUrl/get
router.get('/originalUrl/get',function(req, res, next){
 res.send(req.originalUrl);
//结果:
/users/originalUrl/get

protocol

使用:req.protocol

//users/protocol/get
router.get('/protocol/get',function(req, res, next){
 res.send(req.protocol);
//结果:

secure

使用:req.secure

说明:用来判断协议是否安全,如果是htt凡科抠图,返回的就是true

//users/secure/get
router.get('/secure/get',function(req, res, next){
 res.send(req.secure);
//结果:
false

xhr

使用:req.xhr

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

//users/xhr/get
router.get('/xhr/get',function(req, res, next){
 res.send(req.xhr);
//结果:
false

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


  • 【H5实例共享】【TGideas】

    原题目:【H5实例共享】【TGideas】H5实例大合集第55波 基本上每一2个礼拜 便会有一个霸屏级的H5实例面世 而这种一个个出色的H5中 有一个影子 隐约可见 静若处子 动若阿姆威尔朗回转加

  • 趣味性小互动交流:我画

    模拟题目:趣味性性小互动交流沟通交流:我画的你毫无疑问猜不了| 第十六期 便于给大家枯燥乏味枯燥的暑期,造成些新鲜趣味性的物件。发展一个新的小频道栏目,这就是趣味性性

  • 连奥运会总冠军都会玩的

    原题目:连奥运会总冠军都会玩的互动交流游戏,今日疯传微信朋友圈! 上星期,家居家具装饰建材制造行业的“杜杜” 一一口气调戏1两个知名品牌 包含互连网、快消品、金融机构、

  • 餐馆怎样借势营销万圣节

    原题目:餐馆怎样借势营销万圣节,这波“鬼好点子”等着你来拿! 依靠国家法定假日开展营销推广主题活动,早已遭受越来越越大餐馆人的高度重视。 一个传统节日营销推广计划方

  • 手机游戏经营的总体目标

    手机游戏圈是个大金矿,根据单款商品的暴发就造就了一系列产品发售企业,我的亲身经历跨过了PC顾客端、网页页面手机游戏、网络游戏的金子时期,其间亲身经历了十分多的工作中