-
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.
add class node how to use url & querystring
- Loading branch information
Showing
2 changed files
with
128 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# node class query string | ||
|
||
## query string | ||
|
||
``` JavaScript | ||
$ node | ||
# 格式化Query对象为字符串( & / =) | ||
> querystring.stringify({name:'mark',class:['node','php'],from:'baidu'}) | ||
'name=mark&class=node&class=php&from=baidu' | ||
# 指定第二个参数格式化Query对象(, / =) | ||
> querystring.stringify({name:'mark',class:['node','php'],from:'baidu'}, ',') | ||
'name=mark,class=node,class=php,from=baidu' | ||
# 指定第二个参数与第三个参数格式化Query对象(, / :) | ||
> querystring.stringify({name:'mark',class:['node','php'],from:'baidu'}, ',', ':') | ||
'name:mark,class:node,class:php,from:baidu' | ||
|
||
# 格式化字符串为Query对象 | ||
> querystring.parse('name=mark&class=node&class=php&from=baidu') | ||
{ name: 'mark', class: [ 'node', 'php' ], from: 'baidu' } | ||
> querystring.parse('name=mark,class=node,class=php,from=baidu', ',') | ||
{ name: 'mark', class: [ 'node', 'php' ], from: 'baidu' } | ||
> querystring.parse('name:mark,class:node,class:php,from:baidu', ',', ':') | ||
{ name: 'mark', class: [ 'node', 'php' ], from: 'baidu' } | ||
|
||
# query 字符串转义与非转义 | ||
> querystring.escape('<哈哈>') | ||
'%3C%E5%93%88%E5%93%88%3E' | ||
> querystring.unescape('%3C%E5%93%88%E5%93%88%3E') | ||
'<哈哈>' | ||
``` |
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,98 @@ | ||
# class url | ||
|
||
## node cli for url | ||
|
||
``` nodejs | ||
# 进入 node 命令行模式 | ||
$ node | ||
# 输入 url ,可以看到url在node中的方法 | ||
> url | ||
{ parse: [Function: urlParse], | ||
resolve: [Function: urlResolve], | ||
resolveObject: [Function: urlResolveObject], | ||
format: [Function: urlFormat], | ||
Url: [Function: Url] } | ||
# 使用url.parse方法解析url | ||
> url.parse('http:https://www.baidu.com:8900/s?word=xxx#maodian') | ||
Url { | ||
protocol: 'http:', | ||
slashes: true, | ||
auth: null, | ||
host: 'www.baidu.com:8900', | ||
port: '8900', | ||
hostname: 'www.baidu.com', | ||
hash: '#maodian', | ||
search: '?word=xxx', | ||
query: 'word=xxx', | ||
pathname: '/s', | ||
path: '/s?word=xxx', | ||
href: 'http:https://www.baidu.com:8900/s?word=xxx#maodian' } | ||
# url格式化对象Query转换为对象形式 | ||
> url.parse('http:https://www.baidu.com:8900/s?word=xxx#maodian', true) | ||
Url { | ||
protocol: 'http:', | ||
slashes: true, | ||
auth: null, | ||
host: 'www.baidu.com:8900', | ||
port: '8900', | ||
hostname: 'www.baidu.com', | ||
hash: '#maodian', | ||
search: '?word=xxx', | ||
query: { word: 'xxx' }, # 注意这里的变化 | ||
pathname: '/s', | ||
path: '/s?word=xxx', | ||
href: 'http:https://www.baidu.com:8900/s?word=xxx#maodian' } | ||
# 针对未指定协议的url进行解析 [注意下面两个有与没有第三个参数的区别] | ||
> url.parse('//www.baidu.com:8900/s?word=xxx#maodian', true) | ||
Url { | ||
protocol: null, | ||
slashes: null, | ||
auth: null, | ||
host: null, | ||
port: null, | ||
hostname: null, | ||
hash: '#maodian', | ||
search: '?word=xxx', | ||
query: { word: 'xxx' }, | ||
pathname: '//www.baidu.com:8900/s', | ||
path: '//www.baidu.com:8900/s?word=xxx', | ||
href: '//www.baidu.com:8900/s?word=xxx#maodian' } | ||
> url.parse('//www.baidu.com:8900/s?word=xxx#maodian', true, true) | ||
Url { | ||
protocol: null, | ||
slashes: true, | ||
auth: null, | ||
host: 'www.baidu.com:8900', | ||
port: '8900', | ||
hostname: 'www.baidu.com', | ||
hash: '#maodian', | ||
search: '?word=xxx', | ||
query: { word: 'xxx' }, | ||
pathname: '/s', | ||
path: '/s?word=xxx', | ||
href: '//www.baidu.com:8900/s?word=xxx#maodian' } | ||
# 将url格式化的对象转换成url | ||
> url.format({ | ||
... protocol: 'http:', | ||
... slashes: true, | ||
... auth: null, | ||
... host: 'www.baidu.com:8900', | ||
... port: '8900', | ||
... hostname: 'www.baidu.com', | ||
... hash: '#maodian', | ||
... search: '?word=xxx', | ||
... query: 'word=xxx', | ||
... pathname: '/s', | ||
... path: '/s?word=xxx', | ||
... href: 'http:https://www.baidu.com:8900/s?word=xxx#maodian' }) | ||
'http:https://www.baidu.com:8900/s?word=xxx#maodian' | ||
# url路径的拼接 | ||
> url.resolve('http:https://www.baidu.com/', 's?word=xxx#maodian') | ||
'http:https://www.baidu.com/s?word=xxx#maodian' | ||
``` |