阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js url.protocol

      Node.js url.protocol

      • < string> 获取及设置URL的协议(protocol)部分。
      const { URL } = require('url');
      const myURL = new URL('https://example.org');
      console.log(myURL.protocol);
        // 输出 https:
      
      myURL.protocol = 'ftp';
      console.log(myURL.href);
        // 输出 ftp://example.org/
      

      如果给protocol属性设置的值是无效值,那么该值将被忽略。

      特殊协议

      The WHATWG URL Standard considers a handful of URL protocol schemes to be special in terms of how they are parsed and serialized. When a URL is parsed using one of these special protocols, the url.protocol property may be changed to another special protocol but cannot be changed to a non-special protocol, and vice versa.

      For instance, changing from http to https works:

      const u = new URL('http://example.org');
      u.protocol = 'https';
      console.log(u.href);
      // https://example.org
      

      However, changing from http to a hypothetical fish protocol does not because the new protocol is not special.

      const u = new URL('http://example.org');
      u.protocol = 'fish';
      console.log(u.href);
      // http://example.org
      

      Likewise, changing from a non-special protocol to a special protocol is also not permitted:

      const u = new URL('fish://example.org');
      u.protocol = 'http';
      console.log(u.href);
      // fish://example.org
      

      The protocol schemes considered to be special by the WHATWG URL Standard include: ftp, file, gopher, http, https, ws, and wss.


      更多内容请参考:Node.js URL,或者通过 点击对应菜单 进行查看;


      目录
      本文目录
      目录