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. GPLT L2-004 这是二叉搜索树吗?

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805070971912192 类似题目有FBI树 这两个题有个小 ...

  2. 第二阶段——个人工作总结DAY01

    今天开始第二阶段的冲刺. 今天准备做什么:首先要先学习活动之间通过TextView用Intent实现跳转. 遇到的困难:无.

  3. 整合多个网络的拓扑结构并降维(Mashup)

    整合多个网络的拓扑结构并降维(Mashup) 介绍一个整合多个网络拓扑结构的方法,方法来源:Compact Integration of Multi-Network Topology for Func ...

  4. php获取当月天数及当月第一天及最后一天

    1.获取上个月第一天及最后一天.   echo date('Y-m-01', strtotime('-1 month'));   echo "<br/>";   ech ...

  5. openssl修改版本号

    1.查看当前openssl版本号 openssl version 2.查看openssl所在位置 which openssl 3.查看保存版本号的libcrypto.so所在位置 ldd /usr/b ...

  6. linux定时删除文件脚本

    #! /bin/sh # 配置项DEBUG=truefolderDir=/var/www/html/hlsrecord/EXPIRE_DAY=1 # 过期时间和时间戳deadTime=`date -d ...

  7. DLL的Export和Import及extern "C"

    今天使用Unrar.dll,在调用RARProcessFileW时,VS总是提示“error LNK2001: 无法解析的外部符号”. Unrar.dll中是使用 extern "C&quo ...

  8. Win10系列:UWP界面布局进阶5

    提示框 在Windows应用商店应用程序中可以使用提示框来向用户显示提示信息,例如可以通过对话框来询问用户当前需要执行的操作,还可以通过弹出窗口来显示需要注意的信息.本节将向读者介绍如何在Window ...

  9. day04_python_1124

    01 上周内容回顾 int bool str int < --- > str: i1 = 100    str(i1) s1 = '10'    int(s1)字符串必须是数字组成. in ...

  10. day05列表 类型

    基本使用 1用途:记录多个值,比如人的多个爱好 # ======================================基本使用================================ ...