Nodejs - url

parse() / new URL()

都是用来解析url的:
用法:
1、parse()

const url = require("url");
const urlString = "http://www.baidu.com:443/path/index.html?id=2#tag=3";
logger.debug(url.parse(urlString));

结果是这样的:

[2022-01-22T20:43:46.471] [DEBUG] cheese - Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'www.baidu.com:443',
  port: '443',
  hostname: 'www.baidu.com',
  hash: '#tag=3',
  search: '?id=2',
  query: 'id=2',
  pathname: '/path/index.html',
  path: '/path/index.html?id=2',
  href: 'http://www.baidu.com:443/path/index.html?id=2#tag=3'
}

2、new URL()

const urlString = "http://www.baidu.com:443/path/index.html?id=2#tag=3";
logger.debug(new URL(urlString));

出来是这样的:

[2022-01-22T20:43:17.041] [DEBUG] cheese - URL {
  href: 'http://www.baidu.com:443/path/index.html?id=2#tag=3',
  origin: 'http://www.baidu.com:443',
  protocol: 'http:',
  username: '',
  password: '',
  host: 'www.baidu.com:443',
  hostname: 'www.baidu.com',
  port: '443',
  pathname: '/path/index.html',
  search: '?id=2',
  searchParams: URLSearchParams { 'id' => '2' },
  hash: '#tag=3'
}

区别:parse() 是 Node.js 特定的旧版 API,new URL() 是实现了与 Web 浏览器使用的相同的 WHATWG 网址标准的新版 API

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值