1. url.parse(网址): 将字符串 解析成对象.

 1-1) 一个参数 : 或者  参数1, false(默认), false(默认)

var url = require('url');

console.log(url.parse("http://www.baidu.com:8080/list?c=Cate&a=index#main"));
console.log(url.parse("https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash"));
console.log(url.parse("http://127.0.0.1/culture/action.php?c=HCourseProxy&a=displaySuccess&courseId=4"));

运行:

--------------------------------------------------

 1-2) 两个参数  或者  是  参数1,  true, false(默认)

如果第二个参数设置为 true. 那么 query属性  就会调用 querystring模块的 方法 ,生成一个对象.

 var url = require('url');

 console.log(url.parse("http://www.baidu.com:8080/list?c=Cate&a=index#main", true));
console.log(url.parse("https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash", true));
console.log(url.parse("http://127.0.0.1/culture/action.php?c=HCourseProxy&a=displaySuccess&courseId=4", true));

运行:

1-3) 三个参数:  参数1 , 参数2(布尔值), true

对于 不知道是什么协议的时候, 如果想解析//www.baidu.com:8080/list?c=Cate&a=index#main 这种 , 就可以将第 三个参数设置为 true.

 var url = require('url');

 console.log(url.parse("//www.baidu.com:8080/list?c=Cate&a=index#main"));
console.log(url.parse("//www.baidu.com:8080/list?c=Cate&a=index#main", false, true)); console.log(url.parse("//baidu.com:8080/list?c=Cate&a=index#main"));
console.log(url.parse("//baidu.com:8080/list?c=Cate&a=index#main", false, true));

1-4) 实例:

第一: 入口文件:  index.js

1 var server = require('./server');

 server.start();
require('./server'); 引入的是 自己定义的本地模块 . 这里 是   ./server

第二: 服务器文件: server.js

 var http = require("http");
var url = require("url"); function start(){
http.createServer(function(request, response) { console.log(url.parse(request.url));
console.log(url.parse(request.url).query);
var pathname = url.parse(request.url).pathname;
console.log('request for [' +pathname+ "] received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888); console.log("server has started.");
} exports.start = start;

访问:

浏览器访问:

结果控制台输出:

不同的网址的输出: pathname, pathname.query .   pathname.pathname

-------

-------

----

2. url.format(对象)  将对象 转成 字符串

var url = require('url');

console.log(url.format({
protocol: 'https:',
slashes: true,
auth: 'user:pass',
host: 'sub.host.com:8080',
port: '8080',
hostname: 'sub.host.com',
hash: '#hash',
search: '?query=string',
query: 'query=string',
pathname: '/p/a/t/h',
path: '/p/a/t/h?query=string',
href: 'https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash' }));

运行:

3.  url.resolve()

 var url = require('url');

 console.log(url.resolve("http://www.runoob.com/", "nodejs/nodejs-path-module.html"));
console.log(url.resolve("http://www.runoob.com/", "/nodejs/nodejs-path-module.html")); console.log(url.resolve("http://www.runoob.com/list/", "nodejs/nodejs-path-module.html"));
console.log(url.resolve("http://www.runoob.com/list/", "/nodejs/nodejs-path-module.html"));
console.log(url.resolve("http://www.runoob.com/list", "/nodejs/nodejs-path-module.html")); console.log(url.resolve("http://www.runoob.com/list/cate/", "../nodejs-path-module.html"));
console.log(url.resolve("http://www.runoob.com/list/cate", "../nodejs-path-module.html"));

运行:

可见:

1)当 第一个参数只是 域名的时候, 第二个参数总是追加在 第一个参数的后面 , 参见 代码 3 ,代码 4

2)当第一个参数 不只是域名,还有path 的时候, 第一个参数末尾是否带有 / , 以及 第二个参数开始是否有/ .都 会对生成的 url地址产生影响.

运行:

--------------

NodeJs -- URL 模块.的更多相关文章

  1. nodejs之url模块

    鄙人初步学习nodejs,目前在读<nodejs入门>这一本书,书很小,但是让我知道了如何用nodejs创建一个简单的小项目.例如如何创建一个服务器啦,例如http.createServe ...

  2. nodejs学习笔记二(get请求、post请求、 querystring模块,url模块)

    请求数据 前台:form.ajax.jsonp 后台:接受请求并返回响应数据     前台<= http协议 =>后台   常用的请求的方式: 1.GET           数据在url ...

  3. 引用nodejs的url模块实现url路由功能

    我们在本地创建服务器之后需要写不同的后缀名来访问同一个站点的不同页面,如果不实现路由功能.则每次访问localhost:3000 不论后面写什么  比如localhost:3000/index.loc ...

  4. nodejs笔记之路由及util和url模块

    路由是URL到函数的映射:对于最简单的静态资源服务器,可以认为,所有URL的映射函数就是一个文件读取操作.对于动态资源,映射函数可能是一个数据库读取操作,也可能是进行一些数据的处理,等等. 如: /u ...

  5. NodeJS 笔记 URL模块

    url模块 ,包含分析和解析 URL 的工具. var url = require('url'); url.parse(urlStr[, parseQueryString][, slashesDeno ...

  6. nodejs入门API之url模块+querystring模块

    关于URL的一些基础内容 URL模块的API解析 URL的参数URLSearchParams类 querystring模块 一.关于URL的一些基础内容 1.1 定义: 在WWW上,每一信息资源都有统 ...

  7. NodeJS http 模块

    #4 NodeJS http 模块 工作目录 server.js var http = require('http'); var fs = require('fs'); var path = requ ...

  8. NodeJS Web模块

    NodeJS Web模块 本文介绍nodeJS的http模块的基本用法,实现简单服务器和客户端 经典Web架构 Client:客户端一般指浏览器,通过HTTP协议向服务器发送请求(request) S ...

  9. node(03)--利用 HTTP 模块 URl 模块 PATH 模块 FS 模块创建一个 WEB 服务器

    Web 服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序,可以向浏览器等 Web 客户端提供文档,也可以放置网站文件,让全世界浏览:可以放置数据文件,让全世界下载.目前最主流的三个 We ...

随机推荐

  1. ZCU板级调试Bug记录

    本帖用以记录在ZCU102板级调试间遇到的Bug. 1.PL端的AXI总线在读取DDR中的数据的时候,在一个burst内不能跨越page boundary.跨越page boundary会在该burs ...

  2. 跨域和jsonp的原理

    什么是跨域? 跨域,指的是浏览器不能执行其他网站的脚本.它是由浏览器的同源策略造成的,是浏览器对JavaScript施加的安全限制. 所谓同源是指,域名,协议,端口均相同,不明白没关系,举个栗子: h ...

  3. find xss

    from :http://blog.sina.com.cn/s/blog_68d342d90100nh7b.html 什么是XSS跨站攻击: http://baike.baidu.com/view/5 ...

  4. hadoopMR自定义输入类型

    hadoop中的输入输出数据类型: BooleanWritable:标准布尔型数值 ByteWritable:单字节数值 DoubleWritable:双字节数值 FloatWritable:浮点数 ...

  5. Leetcode 144

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  6. Mac实现远程服务器登录管理

    在MAC上还没发现xshell一样的终端工具.不过mac的终端命令,可以写好多脚本来管理. 首先想到的就是把服务器IP及密码使用shell管理起来,以便下次登录的时候不用在重新输入,或者只输入代号就行 ...

  7. GSON使用之对特殊字符的转换的处理

    很多人是在转换时特殊字符被替换成了unicode编程格式,而我碰到的类似,只不过是后台转换成json字符串到前端,前端解析时 '' 双引号和 / 斜杠被原样转换,冲突了json的关键字符,导致解析时提 ...

  8. servlet-api-2.4.jar not loaded(转)

    信息: validateJarFile(D:/xj/workspace/webworktest/webapp/WEB-INF/lib/servlet-api-2.4.jar) - jar not lo ...

  9. Spring注解之@Retention

    作用是定义被它所注解的注解保留多久,一共有三种策略,定义在RetentionPolicy枚举中: package java.lang.annotation; /** * Annotation rete ...

  10. hdu-1176免费馅饼

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...