nodejs url模块详解

nodejs url模块

nodejs中用户url格式化和反格式化模块
用于url解析、处理等操作的解决方案

1.url.parse(urlString[, parseQueryString[, slashesDenoteHost]])

  • urlString <string> 要解析的 URL 字符串。
  • parseQueryString <boolean> 如果为 true,则 query 属性总会通过 querystring 模块的 parse() 方法生成一个对象。 如果为 false,则返回的 URL 对象上的 query 属性会是一个未解析、未解码的字符串。 默认为 false
  • slashesDenoteHost <boolean> 如果为 true,则 // 之后至下一个 / 之前的字符串会被解析作为 host。 例如,//foo/bar 会被解析为 {host: 'foo', pathname: '/bar'} 而不是 {pathname: '//foo/bar'}。 默认为 false
    url.parse() 方法会解析一个 URL 字符串并返回一个 URL 对象。

如果urlString不是字符串将会抛出TypeError。

如果auth属性存在但无法编码则抛出URIError。

示例1:

var url = require("url")
var myurl="http://www.nodejs.org/some/url/?with=query&param=that#about"
parsedUrl=url.parse(myurl)

结果

{ protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'www.nodejs.org',
  port: null,
  hostname: 'www.nodejs.org',
  hash: '#about',
  search: '?with=query&param=that',
  query: 'with=query&param=that',
  pathname: '/some/url/',
  path: '/some/url/?with=query&param=that',
  href: 'http://www.nodejs.org/some/url/?with=query&param=that#about' 
}

当parse方法第二个参数为true时,结果如下

parsedUrl=url.parse(myurl,true)
{ protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'www.nodejs.org',
  port: null,
  hostname: 'www.nodejs.org',
  hash: '#about',
  search: '?with=query&param=that',
  query: { with: 'query', param: 'that' },
  pathname: '/some/url/',
  path: '/some/url/?with=query&param=that',
  href: 'http://www.nodejs.org/some/url/?with=query&param=that#about' }

2.url.format(urlObject)

  • urlObject <Object> | <string> 一个 URL 对象(就像 url.parse() 返回的)。 如果是一个字符串,则通过 url.parse() 转换为一个对象。

url.format() 方法返回一个从 urlObject 格式化后的 URL 字符串。

如果 urlObject 不是一个对象或字符串,则 url.format() 抛出 TypeError

示例

var url=require('url');  
var obj1={ protocol: 'http:',      
  slashes: true,         
  auth: null,           
  host: 'calc.gongjuji.net',   
  port: null,                 
  hostname: 'calc.gongjuji.net',  
  hash: '#one#two',              
  search: '?name=zhangsan&age=18',  
  query: 'name=zhangsan&age=18',    
  pathname: '/byte/',              
  path: '/byte/?name=zhangsan&age=18',  
  href: 'http://calc.gongjuji.net/byte/?name=zhangsan&age=18#one#two'   
};  
var url1=url.format(obj1);  
console.log(url1);//http://calc.gongjuji.net/byte/?name=zhangsan&age=18#one#two  
//请求参数为为json对象  
var obj2={ protocol: 'http:',  
slashes: true,  
auth: null,  
host: 'calc.gongjuji.net',  
port: null,  
hostname: 'calc.gongjuji.net',  
hash: '#one#two',  
search: '?name=zhangsan&age=18',  
query: { name: 'zhangsan', age: '18' }, //页面参数部分,已经解析成对象了  
pathname: '/byte/',  
path: '/byte/?name=zhangsan&age=18',  
href: 'http://calc.gongjuji.net/byte/?name=zhangsan&age=18#one#two' };  
var url2=url.format(obj2);  
console.log(url2); //http://calc.gongjuji.net/byte/?name=zhangsan&age=18#one#two  
//缺少参数的情况  
var obj3={ protocol: null,  
slashes: true,  
auth: null,  
host: 'www.gongjuji.net',  
port: null,  
hostname: 'www.gongjuji.net',  
hash: '#one',  
search: '?name=zhangsan',  
query: { name: 'zhangsan' },  
pathname: '/byte/',  
path: '/byte/?name=zhangsan',  
href: '//www.gongjuji.net/byte/?name=zhangsan#one' };  
var url3=url.format(obj3);  
console.log(url3);//www.gongjuji.net/byte/?name=zhangsan#one  

3.url.resolve(from, to)

  • from <string> 解析时相对的基本 URL。
  • to <string> 要解析的超链接 URL。

url.resolve() 方法会以一种 Web 浏览器解析超链接的方式把一个目标 URL 解析成相对于一个基础 URL。

例子

url.resolve('/one/two/three', 'four')         // '/one/two/four'
url.resolve('http://example.com/', '/one')    // 'http://example.com/one'
url.resolve('http://example.com/one', '/two') // 'http://example.com/two'

参考:
https://www.jianshu.com/p/fb5278d02cc4
http://nodejs.cn/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost
https://blog.csdn.net/u011127019/article/details/52350172

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • Node.js Node.js 就是运行在服务端的 JavaScript。 Node.js 是一个基于Chrome...
    Gukson666阅读 700评论 0 1
  • 个人入门学习用笔记、不过多作为参考依据。如有错误欢迎斧正 目录 简书好像不支持锚点、复制搜索(反正也是写给我自己看...
    kirito_song阅读 2,500评论 1 37
  • 最近认识了一位姐姐,她今年35岁。 对于一位女性来说,这真的算不上一个好年龄。没办法拥有年轻时的勇气,也没办法像已...
    夏天醒了阅读 509评论 0 0
  • 她是南方姑娘,性格温婉、以写字为生。她是80后畅销书作家独木舟,真名叫葛婉仪。从2005年至今,她已经出版多部长篇...
    悦恩阅读 8,986评论 29 82