forked from dunizb/CodeTest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
url模块常用方法 QueryString模块用法
- Loading branch information
Showing
3 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// =================== 序列化 ===================== | ||
|
||
var qs = require('querystring'); | ||
|
||
var res = qs.stringify({name:'dunizb',course:['jade','node'],from:''}); | ||
// console.log(res); | ||
// name=dunizb&course=jade&course=node&from= | ||
|
||
//第二个参数指定分隔符 | ||
var res = qs.stringify({name:'dunizb',course:['jade','node'],from:''},'-'); | ||
// console.log(res); | ||
// name=dunizb-course=jade-course=node-from= | ||
|
||
//第三个参数指定key,value之间的符号 | ||
var res = qs.stringify({name:'dunizb',course:['jade','node'],from:''},'-',':'); | ||
// console.log(res); | ||
// name:dunizb-course:jade-course:node-from: | ||
|
||
|
||
// ================== 反序列化 ======================= | ||
|
||
var res = qs.parse('name=dunizb&course=jade&course=node&from='); | ||
// console.log(res); | ||
// { name: 'dunizb', course: [ 'jade', 'node' ], from: '' } | ||
|
||
var res = qs.parse('name=dunizb-course=jade-course=node-from=','-'); | ||
// console.log(res); | ||
// { name: 'dunizb', course: [ 'jade', 'node' ], from: '' } | ||
|
||
var res = qs.parse('name:dunizb-course:jade-course:node-from:','-',':'); | ||
// console.log(res); | ||
// { name: 'dunizb', course: [ 'jade', 'node' ], from: '' } | ||
|
||
// ================== 转义与反转义 ======================= | ||
var res = qs.escape('<哈哈>'); | ||
// console.log(res); | ||
// %3C%E5%93%88%E5%93%88%3E | ||
|
||
var res = qs.unescape('%3C%E5%93%88%E5%93%88%3E'); | ||
// console.log(res); | ||
// <哈哈> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// =================== URI与URL的区别? ======================== | ||
// URI属于URL更高层次的抽象,一种字符串文本标准。就是说,URI属于父类,而URL属于URI的子类。URL是URI的一个子集。 | ||
// 二者的区别在于,URI表示请求服务器的路径,定义这么一个资源。而URL同时说明要如何访问这个资源(http://)。 | ||
// URI可以表示一个域,也可以表示一个资源。URL只能表示一个资源。 | ||
|
||
var url = require('url'); | ||
|
||
// 解析URL字符串为对象 | ||
var res = url.parse("http://www.imooc.com/search/course?words=node"); | ||
// console.log(res); | ||
// Url { | ||
// protocol: 'http:', | ||
// slashes: true, | ||
// auth: null, | ||
// host: 'www.imooc.com', | ||
// port: null, | ||
// hostname: 'www.imooc.com', | ||
// hash: null, | ||
// search: '?words=node', | ||
// query: 'words=node', | ||
// pathname: '/search/course', | ||
// path: '/search/course?words=node', | ||
// href: 'http://www.imooc.com/search/course?words=node' } | ||
|
||
var res = url.parse("http://www.imooc.com/search/course?words=node",true); | ||
// console.log(res); | ||
// Url { | ||
// protocol: 'http:', | ||
// slashes: true, | ||
// auth: null, | ||
// host: 'www.imooc.com', | ||
// port: null, | ||
// hostname: 'www.imooc.com', | ||
// hash: null, | ||
// search: '?words=node', | ||
// query: { words: 'node' }, | ||
// pathname: '/search/course', | ||
// path: '/search/course?words=node', | ||
// href: 'http://www.imooc.com/search/course?words=node' } | ||
|
||
var urlobj = { | ||
protocol: 'http:', | ||
slashes: true, | ||
auth: null, | ||
host: 'www.imooc.com', | ||
port: null, | ||
hostname: 'www.imooc.com', | ||
hash: null, | ||
search: '?words=node', | ||
query: { words: 'node' }, | ||
pathname: '/search/course', | ||
path: '/search/course?words=node', | ||
href: 'http://www.imooc.com/search/course?words=node' } | ||
|
||
// 格式化字符串对象为URL | ||
var res = url.format(urlobj); | ||
// console.log(res); | ||
// http://www.imooc.com/search/course?words=node | ||
|
||
var res = url.resolve("http://www.imooc.com/","search/course?words=node"); | ||
// console.log(res); | ||
// http://www.imooc.com/search/course?words=node | ||
|