您的位置:首页 > Web前端 > Node.js

node.js url模块

2015-09-08 22:54 579 查看
URL

url.parse(urlStr[, parseQueryString][, slashesDenoteHost])

url.format(urlObj)

url.resolve(from, to)

具体地址:http://nodejs.cn/api/url.html

3种引用模式

C:\Documents and Settings\Administrator\WebstormProjects\untitled3>node

先进入node环境

> url
{ parse: [Function: urlParse],
resolve: [Function: urlResolve],
resolveObject: [Function: urlResolveObject],
format: [Function: urlFormat],
Url: [Function: Url] }

调用url

parse解析:

url.parse(urlStr[, parseQueryString][, slashesDenoteHost])

> url.parse('http://www.imooc.com/video/6710')

//生成项:
{ protocol: 'http:',
slashes: true,
auth: null,
host: 'www.imooc.com',
port: null,
hostname: 'www.imooc.com',
hash: null,
search: null,
query: null,
pathname: '/video/6710',
path: '/video/6710',
href: 'http://www.imooc.com/video/6710' }

format解析:你给予的信息生成一个url

url.format(urlObj)

> url.format({ protocol: 'http:',
... slashes: true,
... auth: null,
... host: 'www.imooc.com',
... port: null,
... hostname: 'www.imooc.com',
... hash: null,
... search: null,
... query: null,
... pathname: '/video/6710',
... path: '/video/6710',
... href: 'http://www.imooc.com/video/6710' }
... )
//生成项:'http://www.imooc.com/video/6710'

resolve解析

url.resolve(from, to)

> url.resolve('http://imooc.com/','/course/list')
//生成项:'http://imooc.com/course/list'

parse的应用

> url.parse('http://imooc.com:8080/curse/list?from=scott&&course=node#dloor1')
{ protocol: 'http:',
slashes: true,
auth: null,
host: 'imooc.com:8080',
port: '8080',
hostname: 'imooc.com',
hash: '#dloor1',
search: '?from=scott&&course=node',
query: 'from=scott&&course=node',
pathname: '/curse/list',
path: '/curse/list?from=scott&&course=node',
href: 'http://imooc.com:8080/curse/list?from=scott&&course=node#dloor1' }

> url.parse('http://imooc.com:8080/curse/list?from=scott&&course=node#dloor1',true)
{ protocol: 'http:',
slashes: true,
auth: null,
host: 'imooc.com:8080',
port: '8080',
hostname: 'imooc.com',
hash: '#dloor1',
search: '?from=scott&&course=node',
query: { from: 'scott', '': '', course: 'node' }, //query被解析成了对象
pathname: '/curse/list',
path: '/curse/list?from=scott&&course=node',
href: 'http://imooc.com:8080/curse/list?from=scott&&course=node#dloor1' }

互相对比下看看那里不一样

> url.parse('//imooc.com/curse/liast',true)
{ protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '',
query: {},
pathname: '//imooc.com/curse/liast',
path: '//imooc.com/curse/liast',
href: '//imooc.com/curse/liast' }

> url.parse('//imooc.com/curse/liast',true,true)
{ protocol: null,
slashes: true,
auth: null,
host: 'imooc.com',
port: null,
hostname: 'imooc.com',
hash: null,
search: '',
query: {},
pathname: '/curse/liast',
path: '/curse/liast',
href: '//imooc.com/curse/liast' }

protocol : 协议
slashes:   是否有协议的双斜线
auth:
host:    域名/ip地址
port:    端口
hostname: 主机名
hash:    面向某个锚点
search: 查询字符串参数
query:   发送给http的一个参数 =号分支开的参数串
pathname:访问资源路径名
path:   路径
href:    超链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: