nodejs url模块详解

nodejs url模块

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

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

  • urlString 要解析的 URL 字符串。
  • parseQueryString 如果为 true,则 query 属性总会通过 querystring 模块的 parse() 方法生成一个对象。 如果为 false,则返回的 URL 对象上的 query 属性会是一个未解析、未解码的字符串。 默认为 false
  • slashesDenoteHost 如果为 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¶m=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¶m=that',
  query: 'with=query¶m=that',
  pathname: '/some/url/',
  path: '/some/url/?with=query¶m=that',
  href: 'http://www.nodejs.org/some/url/?with=query¶m=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¶m=that',
  query: { with: 'query', param: 'that' },
  pathname: '/some/url/',
  path: '/some/url/?with=query¶m=that',
  href: 'http://www.nodejs.org/some/url/?with=query¶m=that#about' }

2.url.format(urlObject)

  • urlObject | 一个 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 解析时相对的基本 URL。
    • to 要解析的超链接 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

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