supman
V2EX  ›  问与答

nodejs 中 url.parse().pathname 这个 pathname 是什么呢? 函数?属性?方法?

  •  
  •   supman · Aug 8, 2017 · 5122 views
    This topic created in 3225 days ago, the information mentioned may be changed or developed.
    7 replies    2017-08-09 08:50:41 +08:00
    imcj
        1
    imcj  
       Aug 8, 2017 via iPhone
    文档看过了?
    supman
        2
    supman  
    OP
       Aug 9, 2017
    @imcj 本来木有看。。直接看的在线教程 https://www.nodebeginner.org

    不过问这个问题之前 也搜索了下文档 https://nodejs.org/api/url.html#url_url_pathname 介绍了这个东西的用途 但并没有说明这是个什么东东。。
    orzfly
        3
    orzfly  
       Aug 9, 2017
    属性。url.parse("...") 的返回是一个对象,pathname 是这个对象的一个属性。

    ```
    orzfly@mirai:~$ irb
    irb(main):001:0> url.parse()^C
    irb(main):001:0> exit
    orzfly@mirai:~$ node
    > url.parse("https://v2ex.xtra.eu.org/t/381509#reply2")
    Url {
    protocol: 'https:',
    slashes: true,
    auth: null,
    host: 'v2ex.xtra.eu.org',
    port: null,
    hostname: 'v2ex.xtra.eu.org',
    hash: '#reply2',
    search: null,
    query: null,
    pathname: '/t/381509',
    path: '/t/381509',
    href: 'https://v2ex.xtra.eu.org/t/381509#reply2' }
    >
    (To exit, press ^C again or type .exit)
    >
    ```
    lzhr
        4
    lzhr  
       Aug 9, 2017
    https://nodejs.org/api/url.html#url_url_strings_and_url_objects
    Below it are properties of a WHATWG URL object.
    Trim21
        5
    Trim21  
       Aug 9, 2017 via iPad
    楼上的图不错。。搭车问一下有没有什么类似的生成工具?
    imcj
        6
    imcj  
       Aug 9, 2017 via iPhone
    @supman @lzr 正解

    刚复制的

    const { URL } = require('url');
    const myURL = new URL('https://example.org/abc/xyz?123');
    console.log(myURL.pathname);
    // Prints /abc/xyz

    myURL.pathname = '/abcdef';
    console.log(myURL.href);
    // Prints https://example.org/abcdef?123
    SourceMan
        7
    SourceMan  
       Aug 9, 2017 via iPhone
    这个叫 构成
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3123 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 55ms · UTC 13:29 · PVG 21:29 · LAX 06:29 · JFK 09:29
    ♥ Do have faith in what you're doing.