zoukankan      html  css  js  c++  java
  • url

    URL 字符串与 URL 对象

    一个 URL 字符串是一个结构化的字符串,它包含多个有意义的组成部分。 当被解析时,会返回一个 URL 对象,它包含每个组成部分作为属性。

    url模块提供了两套API来处理URLs:一个是Node.js遗留的特有的API,另一个则是通常使用在web浏览器中 实现了WHATWG URL Standard的API.

    请注意: 虽然Node.js遗留的特有的API并没有被弃用,但是保留的目的是用于向后兼容已有应用程序。因此新的应用程序请使用WHATWG API。

    WHATWG与Node.js遗留的特有的API的比较如下。网址'http://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash'上方是由遗留的url.parse()返回的对象属性。网址下方的则是由WHATWG URL对象的属性。

    WHATWG URL的origin属性包括protocolhost,但不包含usernamepassword.

    ┌─────────────────────────────────────────────────────────────────────────────────────────────┐
    │                                            href                                             │
    ├──────────┬──┬─────────────────────┬─────────────────────┬───────────────────────────┬───────┤
    │ protocol │  │        auth         │        host         │           path            │ hash  │
    │          │  │                     ├──────────────┬──────┼──────────┬────────────────┤       │
    │          │  │                     │   hostname   │ port │ pathname │     search     │       │
    │          │  │                     │              │      │          ├─┬──────────────┤       │
    │          │  │                     │              │      │          │ │    query     │       │
    "  https:   //    user   :   pass   @ sub.host.com : 8080   /p/a/t/h  ?  query=string   #hash "
    │          │  │          │          │   hostname   │ port │          │                │       │
    │          │  │          │          ├──────────────┴──────┤          │                │       │
    │ protocol │  │ username │ password │        host         │          │                │       │
    ├──────────┴──┼──────────┴──────────┼─────────────────────┤          │                │       │
    │   origin    │                     │       origin        │ pathname │     search     │ hash  │
    ├─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────────┴───────┤
    │                                            href                                             │
    └─────────────────────────────────────────────────────────────────────────────────────────────┘
    (请忽略字符串中的空格,它们只是为了格式化)
    

    利用WHATWG API解析一个URL字符串:

    const { URL } = require('url');
    const myURL =
      new URL('https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash');
    

    在浏览器中,WHATWG URL在全局总是可用的,而在Node.js中,任何情况下打开 或使用一个链接都必须事先引用'url'模块:require('url').URL

    通过Node.js提供的API解析一个URL:

    const url = require('url');
    const myURL =
      url.parse('https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash');





  • 相关阅读:
    简单的理解原型链
    react->Context笔记
    工作上git指令小结
    vue 绑定事件如何传递参数的同时拿到事件对象
    vsCode卸载后重新安装,以前的插件有没有效果的解决方法
    mongo 分组 aggregation
    Redisson分布式锁原理
    Virtual server server already has a web module live-mix-1.0.2-t230 loaded at / therefore web module
    二进制中 1 的个数
    替换空格
  • 原文地址:https://www.cnblogs.com/zgqdbxb/p/7590673.html
Copyright © 2011-2022 走看看