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. PHP字符串函数小结

    1. strlen:获得字符串长度 2. substr:字符串截取函数 格式:string substr ( string $string , int $start [, int $length ] ...

  2. php调用oracle存储

    //todo 调用oracle 存储$config = //数据库配置文件 里面包含 用户密码和host和端口以及dbname $conn = oci_connect($config['usernam ...

  3. WDA基础十三:常用模板管理

    常用的模板一般是SMW0和OAOR,根据不同需求来的. WAD有个不好的地方就是不支持GUI上的OLE和DOI,所以需要做转换,下面是常用的方式: FUNCTION ZCRM_DOWNLOAD_TEM ...

  4. Json:前台对象数组传到后台解析

    本文转自:http://blog.csdn.net/kymegg/article/details/50964581 方法:使用JsonArray进行解析 步骤: 一.引入一系列JAR包 要使程序便于解 ...

  5. [luogu P3195] [HNOI2008]玩具装箱TOY

    [luogu P3195] [HNOI2008]玩具装箱TOY 题目描述 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆, ...

  6. 一些input用法

    //设定选中范围someInput.setSelectionRange(0, 2); //只读<input type="text" value="2" r ...

  7. 三、存储过程(Stored Procedure)与游标(Cursor)

    一.存储过程 一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给出参数,来执行它. 在大型数据库中,存储过程和触发器具有重要的作用.无论是存储过程还是触发器,都 ...

  8. 前端web的图标的样式

  9. Legal or Not (判断是否存在环)

    Legal or Not Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  10. 利用ML&AI判定未知恶意程序——里面提到ssl恶意加密流检测使用N个payload CNN + 字节分布包长等特征综合判定

    利用ML&AI判定未知恶意程序 导语:0x01.前言 在上一篇ML&AI如何在云态势感知产品中落地中介绍了,为什么我们要预测未知恶意程序,传统的安全产品已经无法满足现有的安全态势.那么 ...