V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
supman
V2EX  ›  问与答

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

  •  
  •   supman · 2017-08-08 23:43:13 +08:00 · 4545 次点击
    这是一个创建于 2442 天前的主题,其中的信息可能已经有所发展或是发生改变。
    7 条回复    2017-08-09 08:50:41 +08:00
    imcj
        1
    imcj  
       2017-08-08 23:53:56 +08:00 via iPhone
    文档看过了?
    supman
        2
    supman  
    OP
       2017-08-09 00:00:47 +08:00
    @imcj 本来木有看。。直接看的在线教程 https://www.nodebeginner.org

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

    ```
    orzfly@mirai:~$ irb
    irb(main):001:0> url.parse()^C
    irb(main):001:0> exit
    orzfly@mirai:~$ node
    > url.parse("https://www.v2ex.com/t/381509#reply2")
    Url {
    protocol: 'https:',
    slashes: true,
    auth: null,
    host: 'www.v2ex.com',
    port: null,
    hostname: 'www.v2ex.com',
    hash: '#reply2',
    search: null,
    query: null,
    pathname: '/t/381509',
    path: '/t/381509',
    href: 'https://www.v2ex.com/t/381509#reply2' }
    >
    (To exit, press ^C again or type .exit)
    >
    ```
    lzhr
        4
    lzhr  
       2017-08-09 00:39:29 +08:00
    https://nodejs.org/api/url.html#url_url_strings_and_url_objects
    Below it are properties of a WHATWG URL object.
    Trim21
        5
    Trim21  
       2017-08-09 01:40:17 +08:00 via iPad
    楼上的图不错。。搭车问一下有没有什么类似的生成工具?
    imcj
        6
    imcj  
       2017-08-09 08:12:14 +08:00 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  
       2017-08-09 08:50:41 +08:00 via iPhone
    这个叫 构成
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5665 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 06:37 · PVG 14:37 · LAX 23:37 · JFK 02:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.