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: 超链接
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: 超链接
相关文章推荐
- node.js 基础学习笔记3 -express
- leetcode Remove Nth Node From End of List java编程学习(七)
- [root@master java]# hadoop namenode -format DEPRECATED: Use of this script to execute hdfs command i
- Populating Next Right Pointers in Each Node II
- node-xlsx使用教程
- 最新原生nodejs调试器的使用大全详解
- HTML5+NodeJs实现WebSocket即时通讯
- nodejs exports和module.exports的区别
- Mac机配置 Sublime Text 用 Node.js 执行 JavaScript 程序
- nodejs: events.js:85
- LeetCode:Swap Nodes in Pairs
- Node.js机制及原理理解初步
- Delete Node in a Linked List
- grunt学习(一)——nodejs入门
- Leetcode Delete Node in a Linked List
- node.js cannot find module
- Reverse Nodes in k-Group
- nodeName和tagName
- nodejs 初步使用以及实现文件上传的功能
- Remove Nth Node From End of List