博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nodejs获取当前url和url参数值
阅读量:7283 次
发布时间:2019-06-30

本文共 1661 字,大约阅读时间需要 5 分钟。

//需要使用的模块 http   url当前url   http://localhost:8888/select?aa=001&bb=002var 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); //取到的值是:001
console.log( 
p.pathname);
//取到的值是:/select

下面附上js的获取方法:

当前URL   http://mj_0203.0fees.net/index.php?aa=001&bb=002document.location:        http://mj_0203.0fees.net/index.php?aa=001&bb=002document.URL:             http://mj_0203.0fees.net/index.php?aa=001&bb=002document.location.href:   http://mj_0203.0fees.net/index.php?aa=001&bb=002self.location.href:       http://mj_0203.0fees.net/index.php?aa=001&bb=002top.location.href:        http://mj_0203.0fees.net/index.php?aa=001&bb=002parent.document.location: http://mj_0203.0fees.net/index.php?aa=001&bb=002top.location.hostname:    mj_0203.0fees.netlocation.hostname:        mj_0203.0fees.net

 

转载地址:http://lkkjm.baihongyu.com/

你可能感兴趣的文章
C# 使用Log4Net记录日志(基础篇)
查看>>
转 比较跨语言通讯框架:Apache Thrift和Google Protobuf
查看>>
push代码到github
查看>>
证书是如何打通信任链的?
查看>>
OSS 性能测试经验总结
查看>>
JavaScript权威设计--JavaScript类型,值,变量(简要学习笔记三)
查看>>
解决无法找到"txfile:platformres:msgmgr\msgmgr.htm"
查看>>
【原创】Windows下使用 Eclipse 管理 RabbitMQ 源码之问题解决
查看>>
继LINQ动态组合查询PredicateExtensions讲解
查看>>
[MySQL学习] Innodb change buffer(1)之初识篇
查看>>
页面图层显示顺序
查看>>
数据结构——队列
查看>>
MySQL内核月报 2014.08-MySQL· 捉虫动态·long semaphore waits
查看>>
linux 目录操作函数
查看>>
DT科技评论:第1期(创刊号)
查看>>
Linux 克隆虚拟机引起的“Device eth0 does not seem to be present, delaying initialization”
查看>>
android-async-http框架源码分析
查看>>
【Oracle】v$表和v_$同义词的访问权限
查看>>
用了 CSDN 的 markdown 编辑器吐槽下~~
查看>>
MariaDB newest alpha version Features short VS PostgreSQL 9.3
查看>>