Express之get,pos请求参数的获取
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之get,pos请求参数的获取的更多相关文章
- SpringMVC之请求参数的获取方式
转载出处:https://www.toutiao.com/i6510822190219264516/ SpringMVC之请求参数的获取方式 常见的一个web服务,如何获取请求参数? 一般最常见的请求 ...
- SpringMVC请求参数的获取方式
一.GET请求参数获取 1. 通过HttpServletRequest获取参数 2. 直接方法参数获取 3. RequestParam注解方式获取请求参数 4. Bean方式获取参数 5. Model ...
- Spring 请求方法的调用原理(Controller)和请求参数的获取的原理
1.请求映射原理 所有的请求都会经过DispatcherServlet这个类,先了解它的继承树 本质还是httpServlet 原理图 测试 request请求携带的参数 从requestMapp ...
- springmvc请求路径和请求参数的获取注解- @PathVariable和@RequestParam
@PathVariable和@RequestParam @PathVariable是从路径里面去获取变量,也就是把路径当做变量. @RequestParam是从请求里面获取参数. 如:url:http ...
- 页面间传递前端请求参数和获取参数:Model model,HttpServletRequest request, ModelMap map参数使用与区别
Model model, HttpServletRequest request, ModelMap map声明变量 一.下面的方法是需要将请求发过来的数据(或者说参数)传递到重定向的页面/转发的页面的 ...
- Http协议入门、响应与请求行、HttpServletRequest对象的使用、请求参数获取和编码问题
1 课程回顾 web入门 1)web服务软件作用: 把本地资源共享给外部访问 2)tomcat服务器基本操作 : 启动: %tomcat%/bin/startup.bat 关闭: %tomcat%/ ...
- SpringBoot 拦截器获取http请求参数
SpringBoot 拦截器获取http请求参数-- 所有骚操作基础 目录 SpringBoot 拦截器获取http请求参数-- 所有骚操作基础 获取http请求参数是一种刚需 定义拦截器获取请求 为 ...
- asp.net中Request请求参数的自动封装
这两天在测一个小Demo的时候发现一个很蛋疼的问题----请求参数的获取和封装,例: 方便测试用所以这里是一个很简单的表单. <!DOCTYPE html> <html xmlns= ...
- Django 请求参数
Django 请求参数 1.获取URL路径中的参数 需求:假设用户访问127.0.0.1/user/1/2,你想获取1,2.应该怎么操作呢? (1)未命名参数(位置参数) # 在项目下的urls.py ...
随机推荐
- JS实现静态html页面左侧导航,右侧连接页面
本人前端小菜,想实现左侧导航固定,右侧链接页面,不想用iframe,请问各位有什么好的建议,最好有个demo,谢谢
- ubuntu如何安装Mac主题
1.安装 Gnome 经典桌面 sudo apt-get install gnome-session-fallback 没有安装桌面的可安装 Gnome 桌面: sudo apt-get instal ...
- iOS开发——Localizable.strings
这篇写的不多,但是绝对诚意满满.不会像别人一样,要不不详细,要不罗里吧嗦一堆. 1.创建Localizable.strings文件 Command+N—>iOS—>Resource—> ...
- STM8时钟系统详解
就我个人看来,研究一块单片机,分为新手和老手两种模式,新人迫切的想先用,你得告诉他们怎么样最快的写出一个能跑起来的程序,告诉他们每一个外设的使用方式,老手不同,用的单片机多了外设对于他们而言没太多好奇 ...
- Thinking in scala (4)----阶乘与尾递归
code1: object factorial{ def main(args:Array[String])={ println(factorial(args(0).toInt)) } def fact ...
- python threading模块中对于信号的抓取
最近的物联网智能网关(树莓派)项目中遇到这样一个问题:要从多个底层串口读取发来的数据,并且做出相应的处理,对于每个串口的数据的读取我能想到的可以采用两种方式: 一种是采用轮询串口的方式,例如每3s向每 ...
- Servlet_ResponseHeader
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...
- 创建iwatch 程序选项
include complication :包含自定义表盘事件 include glance scene:包含缩略图事件
- 笔记整理——linux
linux文件目录介绍 (2015/4/30 19:20:28) /proc 目录中的主要文件的说明 文件或目录名称 描 述 apm 高级电源管理信息 cmdline 这个文 ...
- DEV组件LookupEdit,ComboBoxEdit绑定数据源
LookupEdit可以绑定数据表(DataTable)或对象数据组(Object List)作为数据源,下拉窗体可自定显示栏位. 绑定数据源需要设置三个参数:DisplayMember ,Value ...