美文网首页
module--URL

module--URL

作者: Willworkgogogo | 来源:发表于2017-02-24 02:02 被阅读0次

    url 包提供了三个方法
    假设url字符串为:'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'

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

    按url规则拆解urlStr字符串,接收三个参数

    1. urlStr, url字符串(必传)
    2. parseQueryString, bool类型, 默认值false。 对url中query部分解析后输出格式的选择, true的话输出格式由默认的 'query=string' 转为对象格式{ query: 'string' }
    3. slashesDenoteHost, bool类型, 默认值false。
    parse解析获取的对象的结构,这个对认识url的组成也是极好的:
    
    href: The full URL that was originally parsed. Both the protocol and host are lowercased.
    
      Example: 'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'
    
    protocol: The request protocol, lowercased.
    
      Example: 'http:'
    
    slashes: The protocol requires slashes after the colon
    
      Example: true or false
    
    host: The full lowercased host portion of the URL, including port information.
    
      Example: 'host.com:8080'
    
    auth: The authentication information portion of a URL.
    
      Example: 'user:pass'
    
    hostname: Just the lowercased hostname portion of the host.
    
     Example: 'host.com'
    
    port: The port number portion of the host.
    
      Example: '8080'
    
    pathname: The path section of the URL, that comes after the host and before the query, including the initial slash if present.
    
      Example: '/p/a/t/h'
    
    search: The 'query string' portion of the URL, including the leading question mark.
    
      Example: '?query=string'
    
    path: Concatenation of pathname and search.
    
      Example: '/p/a/t/h?query=string'
    
    query: Either the 'params' portion of the query string, or a querystring-parsed object.
    
      Example: 'query=string' or {'query':'string'}
    
    hash: The 'fragment' portion of the URL including the pound-sign.
    
      Example: '#hash'
    

    二、 url.format(urlObj)

    parse的反用,将对象解析成url字符串

    三、 url.resolve(from, to)

    为URL或 href 插入 或 替换原有的标签

    var url = require('url');
    var a = url.resolve('/one/two/three', 'four') ,
    b = url.resolve('http://example.com/', '/one'),
    c = url.resolve('http://example.com/one', '/two');
    console.log(a +","+ b +","+ c);
    //输出结果:
    ///one/two/four
    //http://example.com/one
    //http://example.com/two
    

    相关文章

      网友评论

          本文标题:module--URL

          本文链接:https://www.haomeiwen.com/subject/qjviwttx.html