代码之家  ›  专栏  ›  技术社区  ›  Victor

如何规范化URL?

  •  4
  • Victor  · 技术社区  · 7 年前

    我要处理的情况是,我需要用户输入各种网址(例如:为他们的个人资料)。但是,用户并不总是在 https://example.com 格式。他们可能会插入如下内容:

    • example.com
    • example.com/
    • example.com/somepage
    • 但是有点像 me@example.com 或者其他不应该被接受的事情

    如何将URL规范化为可能导致Web地址的格式?我在web浏览器中看到了这种行为。我们几乎总是在网页浏览器的栏里输入一些糟糕的东西,他们可以区分这是搜索还是可以转换成url的东西。

    我试着找了很多地方,但似乎找不到任何办法。

    如果可能的话,我希望为node编写一个解决方案。非常感谢你!

    2 回复  |  直到 7 年前
        1
  •  3
  •   nowy    7 年前

    使用节点的 URL API,还有一些人工检查。

    1. 手动检查URL是否具有有效的协议。
    2. 实例化url。
    3. 检查url是否不包含其他信息。

    示例代码:

    const { URL } = require('url')
    let myTestUrl = 'https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash';
    
    try {
      if (!myTestUrl.startsWith('https://') && !myTestUrl.startsWith('http://')) {
        // The following line is based on the assumption that the URL will resolve using https.
        // Ideally, after all checks pass, the URL should be pinged to verify the correct protocol.
        // Better yet, it should need to be provided by the user - there are nice UX techniques to address this.
        myTestUrl = `https://${myTestUrl}`
      }
    
      const normalizedUrl = new URL(myTestUrl);
    
      if (normalizedUrl.username !== '' || normalized.password !== '') {
        throw new Error('Username and password not allowed.')
      }
    
      // Do your thing
    } catch (e) {
      console.error('Invalid url provided', e)
    }
    

    我只使用 http https 在这个例子中,为了一个要点。

    直接从文档中,很好地可视化了api:

    ┌─────────────────────────────────────────────────────────────────────────────────────────────┐
    │                                            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                                             │
    └─────────────────────────────────────────────────────────────────────────────────────────────┘
    
        2
  •  0
  •   Sindre Sorhus    6 年前

    你想要的 normalize-url 包裹:

    const normalizeUrl = require('normalize-url');
    
    normalizeUrl('example.com/');
    //=> 'http://example.com'
    

    它在url上运行一系列规范化。

    推荐文章