NodeJs -- URL 模块.
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 模块.的更多相关文章
- nodejs之url模块
鄙人初步学习nodejs,目前在读<nodejs入门>这一本书,书很小,但是让我知道了如何用nodejs创建一个简单的小项目.例如如何创建一个服务器啦,例如http.createServe ...
- nodejs学习笔记二(get请求、post请求、 querystring模块,url模块)
请求数据 前台:form.ajax.jsonp 后台:接受请求并返回响应数据 前台<= http协议 =>后台 常用的请求的方式: 1.GET 数据在url ...
- 引用nodejs的url模块实现url路由功能
我们在本地创建服务器之后需要写不同的后缀名来访问同一个站点的不同页面,如果不实现路由功能.则每次访问localhost:3000 不论后面写什么 比如localhost:3000/index.loc ...
- nodejs笔记之路由及util和url模块
路由是URL到函数的映射:对于最简单的静态资源服务器,可以认为,所有URL的映射函数就是一个文件读取操作.对于动态资源,映射函数可能是一个数据库读取操作,也可能是进行一些数据的处理,等等. 如: /u ...
- NodeJS 笔记 URL模块
url模块 ,包含分析和解析 URL 的工具. var url = require('url'); url.parse(urlStr[, parseQueryString][, slashesDeno ...
- nodejs入门API之url模块+querystring模块
关于URL的一些基础内容 URL模块的API解析 URL的参数URLSearchParams类 querystring模块 一.关于URL的一些基础内容 1.1 定义: 在WWW上,每一信息资源都有统 ...
- NodeJS http 模块
#4 NodeJS http 模块 工作目录 server.js var http = require('http'); var fs = require('fs'); var path = requ ...
- NodeJS Web模块
NodeJS Web模块 本文介绍nodeJS的http模块的基本用法,实现简单服务器和客户端 经典Web架构 Client:客户端一般指浏览器,通过HTTP协议向服务器发送请求(request) S ...
- node(03)--利用 HTTP 模块 URl 模块 PATH 模块 FS 模块创建一个 WEB 服务器
Web 服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序,可以向浏览器等 Web 客户端提供文档,也可以放置网站文件,让全世界浏览:可以放置数据文件,让全世界下载.目前最主流的三个 We ...
随机推荐
- JIT编译器技术理解
参考链接: https://blog.csdn.net/liaodehong/article/details/51605457 https://www.cnblogs.com/insistence/p ...
- HTTP请求/响应报文结构
HTTP协议版本有两种:HTTP1.0和HTTP1.1 它们俩的区别在于:HTTP1.0对于每个连接都只能传送一个请求和响应,请求后就会关闭,HTTP1.0没有Host字段:而HTTP1.1在同一个连 ...
- vivado第一天从建立文件运行小程序开始
今天,是第一天什么也处于懵懂的时候,首要的任务就是建立一个文件 首先打开vivado运行软件, 如图所示,选择第一个create new project 来新建文件 选择存储路径,一路向下 当选择芯片 ...
- zsh切换bash bash切换zsh
切换bash(需要sudo) chsh -s /bin/bash 切换zsh(不需要sudo) chsh -s /bin/zsh 注意:如果输入命令和密码后提示:no change made. 请加上 ...
- python-day71--django多表双下划线查询及分组聚合及F/Q查询
#====================================双下划线的跨表查询===============# 前提 此时 related_name=bookList 属性查询: # 查 ...
- php多线程代码
<?php$thNum = 20; //20个进程$total = 20000;//总数$pageNum=100;//每个页面显示100条数据 $pageCount = ceil($total/ ...
- MI04 盘点单数据录入
*&---------------------------------------------------------------------* *& Report ZRHSSD002 ...
- dp 入门
1.HDU 1003 求最长连续相加的和. dp[i]=max(a[i],dp[i-1]+a[i]); dp[i]表示以 i 结尾的最大值. 再开/个strat去标记从哪里开始. #include & ...
- notepad++自动对齐使用空格代替Tab并将空格显示为小点
一.说明 对大多数语言而言自动对齐使用空格还是tab对编译运行并没有什么影响,但对python问题就很大:因为就算是缩进看起来是一样的但某些行用空格某些行用tab运行会报错. 另外除了空格替换tab外 ...
- linux常用文本编缉命令(strings/sed/awk/cut)
一.strings strings--读出文件中的所有字符串 二.sed--文本编缉 类型 命令 命令说明 字符串替换 sed -i 's/str_reg/str_rep/' filename 将文件 ...