nodejs获取当前url和url参数值
//需要使用的模块 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);
//取到的值是:locahost
console.log(
p.host);
//取到的值是:
localhost
:8888
console.log(
p.port);
//取到的值是:8888
console.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); //取到的值是:001
console.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
nodejs获取当前url和url参数值的更多相关文章
- .net获取当前网址url(各种参数值)
.net获取当前网址url(各种参数值) 假设当前页完整地址是:http://www.test.com/aaa/bbb.aspx?id=1&name=category" 先来看一下整 ...
- JS根据key值获取URL中的参数值,以及把URL的参数转换成json对象
//把url的参数部分转化成json对象 parseQueryString: function (url) { var reg_url = /^[^\?]+\?([\w\W]+)$/, reg_par ...
- 【javascript】js 获取 url 后的参数值
以前写过一篇类似的博文(提取 url 的搜索字符串中的参数),但是个人觉得使用起来不是很方便,今天抽空重新写了个函数,该函数代码更加简洁. //获取 url 后的参数值 function getUrl ...
- JQuery URL的GET参数值获取方法
// jQuery url get parameters function [获取URL的GET参数值] // <code> // var GET = $.urlGet(); //获取UR ...
- 在JQuery中获取URL中的参数值
添加一个js文件,代码如下 // * jQuery url get parameters function [获取URL的GET参数值] // *character_set UTF-8 // * au ...
- javascript获取URL参数和参数值
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- jQuery获取URL的GET参数值
// jQuery url get parameters function [获取URL的GET参数值] // <code> // var GET = $.urlGet(); //获取UR ...
- 用Java和Nodejs获取http30X跳转后的url
用Java和Nodejs获取http30X跳转后的url 转 https://calfgz.github.io/blog/2018/05/http-redirect-java-node.html 30 ...
- [转]js获取域名、url、url参数值
//获取域名host1 = window.location.host;host2 = document.domain; //获取页面完整地址url = window.location.href; 获取 ...
随机推荐
- 开心网的账号登录及api操作
.kaixin.php <?php /** * PHP Library for kaixin001.com * * @author */ class kaixinPHP { function _ ...
- C# NamePipe使用小结
最近在一次项目中使用到了C#中命名管道,所以在此写下一篇小结备忘. 为什么要使用命名管道呢?为了实现两个程序之间的数据交换.假设下面一个场景.在同一台PC上,程序A与程序B需要进行数据通信,此时我们就 ...
- hdu 4043 2011北京赛区网络赛D 概率+大数 **
推出公式为:P = A(2n,n)/(2^(2n)*n!) 但是不会大数,学完java再补
- [Linux] 解压tar.gz文件,解压部分文件
遇到数据库无法查找问题原因,只能找日志,查找日志的时候发现老的日志都被压缩了,只能尝试解压了 数据量比较大,只能在生产解压了,再进行查找 文件名为*.tar.gz,自己博客以前记录过解压方法: h ...
- Matlab tips and tricks
matlab tips and tricks and ... page overview: I created this page as a vectorization helper but it g ...
- MyBatis传入参数与parameterType
参考:http://openwares.net/database/mybatis_parametertype.html Mybatis的Mapper文件中的select.insert.update.d ...
- Sizeof与Strlen的区别与联系(转)
Sizeof与Strlen的区别与联系 一.sizeof sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型 ...
- Linux内核装载和启动一个可执行程序
“平安的祝福 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” 理解编 ...
- C语言连接SQLSERVER数据库
第一步:配置ODBC.在配置ODBC时有用户DSN.系统DSN.和文件DSN三种方法,为了稳妥起见,采用系统DSN. DSN的名字叫LocalServer,帐号:sa,密码123456 第二步:打开V ...
- Attribute在.net编程中的应用
Attribute FYI Link: Attribute在.net编程中的应用(一) Attribute在.net编程中的应用(二) Attribute在.net编程中的应用(三) Attribut ...