取当前的地址栏的Url和url中的参数
看到这样一段代码:
exports.showLogin = function (req, res) {
req.session._loginReferer = req.headers.referer;
res.render('sign/signin');
};
php中可以通过post or get 获取到url中的参数值,最近接触了node那么在nodejs里是怎么做的呢,上代码了!
//需要使用的模块 http url
当前url http://localhost:8888/select?aa=001&bb=002
var http = require('http');
var URL = require('url');
http.createServer(function(req, res){
var arg = url.parse(req.url).query; //方法一arg => aa=001&bb=002
var arg = url.parse(req.url, true).query; //方法二arg => { aa: '001', bb: '002' }
console.log(arg.aa);//返回001
console.log(arg.bb);//返回002
//然后就可以根据所得到的数据处理了
}).listen(8888);//建立服务器并监听端口
获取特定url参数值
var testUrl = 'http://localhost:8888/select?aa=001&bb=002';var p = URL.parse(testUrl); console.log(p.href); //取到的值是:http://localhost:8888/select?aa=001&bb=002console.log(p.protocol); //取到的值是:http: console.log( p.hostname);//取到的值是:locahostconsole.log(p.host);//取到的值是:localhost:8888console.log(p.port);//取到的值是:8888console.log(p.path);//取到的值是:/select?aa=001&bb=002console.log(p.hash);//取到的值是:null console.log(p.query);// 取到的值是:aa=001
在此值得注意的是当语句 是 var p = URL.parse(testUrl, true) 时,p.query则返回的是如:{aa:'001'}这样的对象, 直接打印p.query则返回 [object Object],这时我们可以这样 写: console.log(p.query.aa); //取到的值是:001console.log( p.pathname);//取到的值是:/select下面附上js的获取方法:
当前URL http://mj_0203.0fees.net/index.php?aa=001&bb=002
document.location: http://mj_0203.0fees.net/index.php?aa=001&bb=002
document.URL: http://mj_0203.0fees.net/index.php?aa=001&bb=002
document.location.href: http://mj_0203.0fees.net/index.php?aa=001&bb=002
self.location.href: http://mj_0203.0fees.net/index.php?aa=001&bb=002
top.location.href: http://mj_0203.0fees.net/index.php?aa=001&bb=002
parent.document.location: http://mj_0203.0fees.net/index.php?aa=001&bb=002
top.location.hostname: mj_0203.0fees.net
location.hostname: mj_0203.0fees.net
取当前的地址栏的Url和url中的参数的更多相关文章
- Django(命名URL和URL反向解析)
day67 参考: https://www.cnblogs.com/liwenzhou/articles/8271147.html#autoid-1-4-0 反向解析URL 本 ...
- C# 获取地址栏的地址(URL)
原文地址:http://blog.csdn.net/dingxingmei/article/details/8448009 设当前页完整地址是:http://www.jb51.net/aaa/bbb. ...
- js获取当前地址栏的域名、Url、相对路径和参数以及指定参数
以下代码整理于网络 1.设置或获取对象指定的文件名或路径. window.location.pathname 例:http://localhost:8086/topic/index?topicId=3 ...
- JavaScript------获取url地址中的参数
$(document).ready(function () { //获取地址中的参数(name是字符串) function getParameter(name) { //正则表达式 var reg = ...
- getQueryString----获取url中得参数
/** * 获取url中得参数 * @param name * @returns {null} */ window.getQueryString = function (name) { var reg ...
- jQuery获取URL中的参数
//获取URL地址栏中的参数 function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + &quo ...
- js获取当前域名、Url、相对路径和参数以及指定参数
一.js获取当前域名有2种方法 1.方法一 var domain = document.domain; 2.方法二 var domain = window.location.host; 3.注意问题 ...
- js获取URL中的参数
js获取URL中的一些参数的意思 location对象 含有当前URL的信息. 属性 href 整个URL字符串. protocol 含有URL第一部分的字符串,如http: host 包含有URL中 ...
- nodejs获取当前url和url参数值
//需要使用的模块 http url 当前url http://localhost:8888/select?aa=001&bb=002 var http = require('http ...
随机推荐
- jQuery Validate 表单验证插件----自定义校验结果样式
一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW 访问密码 f224 二.引入依赖包 <script src="../../scripts/j ...
- C++杂谈(一)const限定符与const指针
const限定符 c++有了新的const关键字,用来定义常变量,可以替C语言中的#define.关于const限定符,有以下需要注意: 1.创建后值不再改变 2.作用范围在文件内有效 3.添加ext ...
- 基于Retrotfit2.1+Material Design+ijkplayer开发的一个APP(新闻,gif 动图,视频播放)
此项目主要目的还是为了练习框架的使用,仅供学习用途. 数据来源 新闻 直接用的聚合数据提供的接口:https://www.juhe.cn/docs/api/id/235gif动图 通过jsoup爬的某 ...
- CUDA入门1
1GPUs can handle thousands of concurrent threads. 2The pieces of code running on the gpu are calle ...
- uva 1572 self-assembly ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxQAAANxCAYAAAB9uv94AAAgAElEQVR4nOxdPW7tOpLWFrQGJb72vI ...
- uva 839 not so mobile——yhx
Not so Mobile Before being an ubiquous communications gadget, a mobile was just a structure made of ...
- 给Testerhome测试小道消息做个硬广告
测试小道消息在荔枝FM上的粉丝马上就要超过1k了.还差17个我们就能够开通社区了.欢迎大家都来收听测试小道消息哈--更多精彩内容还希望大家下载荔枝fm.关注FM245329. 如果你还不了解测试小道消 ...
- mysql创建表
说明:此文件包含了blog数据库中建立所有的表的Mysql语句. 在sql语句中注意“约束的概念": 1.实体完整性约束(主键--唯一且非空) primary key() 违约处理:No a ...
- 【hibernate】<第二节>hibernate的一对多映射(基本类型)
所需工具与前文一致! 第一部分内容:基本类型的一对多(one to many) 以部门表与员工表为例: 目录结构: hibernate.cfg.xml内容 <?xml version=" ...
- lcx转发 【解决内网没法链接3389 的问题】
要求我自己在外网 然后监听1111端口,将1111端口数据流量转发至2222端口 被入侵主机上 将本地的2222端[愿3389 主机修改了远程连接的端口]口流量转发至外网ip的1111端口 2222为 ...