首页 > 代码库 > node知识

node知识

node中的url

url中的方法:

parse,resolve,format;

方法parse:

例子:url.parse(‘http://imooc.com/course/list‘);

结果:{

protocol:‘http‘,

slashes:true,

auth:null,

host:‘imooc.com‘,

port:null,

hostname:‘imooc.com‘,

hash:null,

search:null,

query:null,

pathname:‘/course/list‘,

path:‘/course/list‘,

href:‘http://imooc.com/course/list‘

}

url.parse(‘http://imooc.com/course:8080/list?from=scott&course=node#floor1‘);

{

protocol:‘http‘,

slashes:true,

auth:null,

host:‘imooc.com:8080‘,

port:‘8080‘,

hostname:‘imooc.com‘,

hash:‘#floor1‘,

search:‘?from=scott&course=node‘,

query:‘?from=scott&course=node‘,

pathname:‘/course/list‘,

path:‘/course/list?from=scott&course=node‘,

href:‘http://imooc.com/course:8080/list?from=scott&course=node#floor1‘

}

 

url.parse(‘http://imooc.com/course:8080/list?from=scott&course=node#floor1‘,true);

{

protocol:‘http‘,

slashes:true,

auth:null,

host:‘imooc.com:8080‘,

port:‘8080‘,

hostname:‘imooc.com‘,

hash:‘#floor1‘,

search:‘?from=scott&course=node‘,

query:{from:‘scott‘,course:‘node‘},

pathname:‘/course/list‘,

path:‘/course/list?from=scott&course=node‘,

href:‘http://imooc.com/course:8080/list?from=scott&course=node#floor1‘

}

 

 

2.format方法

url.format({

protocol:‘http‘,

slashes:true,

auth:null,

host:‘imooc.com‘,

port:null,

hostname:‘imooc.com‘,

hash:null,

search:null,

query:null,

pathname:‘/course/list‘,

path:‘/course/list‘,

href:‘http://imooc.com/course/list‘

})

结果:‘http://imooc.com/course/list‘

 

3.resolve

url.resolve(‘http://imooc.com/‘,‘/course/list‘);

结果:’http://imooc.com/course/list‘

 

querystring这个对象。

1、querystring.stringify({name:‘scott‘,course:[‘jade‘,‘node‘],from:‘‘})

结果:‘name=scott&course=jade&course=node&from=‘

2、querystring.stringify({name:‘scott‘,course:[‘jade‘,‘node‘],from:‘‘},‘,‘)

结果:‘name=scott,course=jade,course=node,from=‘

3、querystring.stringify({name:‘scott‘,course:[‘jade‘,‘node‘],from:‘‘},‘,‘,‘:‘);

‘name:scotte,course:jade,course:node,from:‘

反序列化

querystring.parse(‘name=scott&course=jade&course=node&from=‘);

结果:

{

name:‘scott‘,

course:[‘jade‘,‘node‘],

from:‘‘

}

querystring.parse(‘name=scott,course=jade,course=node,from=‘,‘,‘);//默认的情况下是以&分开的,第二个参数要告诉的是以什么来解析

结果:

{

name:‘scott‘,

course:[‘jade‘,‘node‘],

from:‘‘

}

 

querystring.parse(‘name:scotte,course:jade,course:node,from:‘,‘,‘,‘:‘);

第三个参数对应的键值对中的连接符

结果一样:

{

name:‘scott‘,

course:[‘jade‘,‘node‘],

from:‘‘

}

 

querystring.escape(‘<哈哈>‘)

‘%3C%E5%93%88%E5%93%88%3E‘

反转义

querystring.unscape(‘%3C%E5%93%88%E5%93%88%3E‘)

结果:‘<哈哈>‘

 

node知识