nodejs的url模块

url.parse() 把url字符串解析成json格式

url.parse('http://www.imooc.com:8080/video/6710?from=zhou&search=koby#xingyuncao')

Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'www.imooc.com:8080',
port: '8080',
hostname: 'www.imooc.com',
hash: '#xingyuncao',
search: '?from=zhou&search=koby',
query: 'from=zhou&search=koby',
pathname: '/video/6710',
path: '/video/6710?from=zhou&search=koby',
href: 'http://www.imooc.com:8080/video/6710?from=zhou&search=koby#xingyuncao' }

url.parse('http://www.imooc.com:8080/video/6710?from=zhou&search=koby#xingyuncao') , 后面第二个参数设置成 true ,会把 query 安装 querystring 方式解析成 json 格式。

![Uploading Paste_Image_071659.png . . .]

nodejs的url模块_第1张图片
Paste_Image.png

默认是 false 格式。

url.parse('http://www.imooc.com:8080/video/6710?from=zhou&search=koby#xingyuncao').query
'from=zhou&search=koby'

url.format() 把json 格式化成 url字符串:

url.format({
... protocol: 'http:',
... slashes: true,
... auth: null,
... host: 'www.imooc.com:8080',
... port: '8080',
... hostname: 'www.imooc.com',
... hash: '#xingyuncao',
... search: '?from=zhou&search=koby',
... query: 'from=zhou&search=koby',
... pathname: '/video/6710',
... path: '/video/6710?from=zhou&search=koby',
... href: 'http://www.imooc.com:8080/video/6710?from=zhou&search=koby#xingyuncao' })

'http://www.imooc.com:8080/video/6710?from=zhou&search=koby#xingyuncao'

url.resolve 拼接:

url.resolve("http://www.sina.com","test")
'http://www.sina.com/test'

querystring.stringify 把json 序列化成一个字符串。

querystring.stringify({name:'green', course:['math','english'], from:'Ted'})
'name=green&course=math&course=english&from=Ted'

第二个参数标示 每个键值对 之间的连接符。
querystring.stringify({name:'green', course:['math','english'], from:'Ted'}, ',')
'name=green,course=math,course=english,from=Ted'

第三个表示键值对的连接符:
querystring.stringify({name:'green', course:['math','english'], from:'Ted'}, ',',':')
'name:green,course:math,course:english,from:Ted'

反序列化:

querystring.parse('name=green&course=math&course=english&from=Ted')
{ name: 'green', course: [ 'math', 'english' ], from: 'Ted' }

你可能感兴趣的:(nodejs的url模块)