cmd 进入命令
node 进入node 环境
1、 url
输入 url 会显示出 url 对象包含的方法
{ parse: [Function: urlParse],
resolve: [Function: urlResolve],
resolveObject: [Function: urlResolveObject],
format: [Function: urlFormat],
Url: [Function: Url] }
1.1 parse 解析地址
url.parse(' http://write.blog.csdn.net/postedit?a=1&b=2#/home ')
解析出:
Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'write.blog.csdn.net',
port: null, //端口
hostname: 'write.blog.csdn.net',
hash: '#/home',
search: '?a=1&b=2',
query: 'a=1&b=2',
pathname: '/postedit',
path: '/postedit?a=1&b=2',
href: 'http://write.blog.csdn.net/postedit?a=1&b=2#/home' }
第二个参数
url.parse(' http://write.blog.csdn.net/postedit?a=1&b=2#/home ' , true) 有第二参数,默认是 false , 加上 true 后里面的 query 参数会变成一个对象
Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'write.blog.csdn.net',
port: null,
hostname: 'write.blog.csdn.net',
hash: '#/home',
search: '?a=1&b=2',
query: { a: '1', b: '2' },
pathname: '/postedit',
path: '/postedit?a=1&b=2',
href: 'http://write.blog.csdn.net/postedit?a=1&b=2#/home' }
第三个参数
url.parse(' //write.blog.csdn.net/postedit?a=1&b=2#/home ' , true , true) //默认是false , 在不知道 是什么开头的情况下(http 或者https)加上第三个参数可正常解析
Url {
protocol: null,
slashes: true,
auth: null,
host: 'write.blog.csdn.net',
port: null,
hostname: 'write.blog.csdn.net',
hash: '#/home',
search: '?a=1&b=2',
query: { a: '1', b: '2' },
pathname: '/postedit',
path: '/postedit?a=1&b=2',
href: '//write.blog.csdn.net/postedit?a=1&b=2#/home' }
url.format( { …以上解析出来的 Url 对象 … } );
会得出 http://write.blog.csdn.net/postedit?a=1&b=2#/home;
1.3 resove
url.resove( 'http://xxx.com/ ', ' /home/1' );
合成一个路径 http://xxx.com//home/1
2、 querystring 对 url 后面的进行操作
2.1querystring.stringify() 把对象数据转换成 url上传参型的数据
在命令里 输入
querystring.stringify({name:'aa',course:['1','2','3'],from:''})
会转换成
'name=aa&course=1&course=2&course=3&from='
第二个参数是 那个 = 号用什么来代替
2.2、 querystring.parse() 把上面转换的解析成原来
querystring.parse('name=aa&course=1&course=2&course=3&from=')
会解析成
{name:'aa',course:['1','2','3'],from:''}
第二个参数是 如果有代替了 & 符的就写上 代替的
第二个参数是 如果有代替了 & 符的就写上 代替的
2.3、 querystring.escape('<呵呵>') , 对url 转义
'%3C%E5%91%B5%E5%91%B5%3E'
2.3 querystring.unescape('%3C%E5%91%B5%E5%91%B5%3E') 转回来
<呵呵>