如何在 Deno 中解析 URL

发布于 2025-01-16 14:48:11 字数 141 浏览 3 评论 0原文

如何解析 deno 中的 URL,如 node.js url.parse ()?

How do you parse a URL in deno like node.js url.parse()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃兔兔 2025-01-23 14:48:11

不需要外部模块来解析 Deno 中的 URL。 URL 类可用作全局的,就像在浏览器中一样:

const urlString = "https://www.google.com";
const url = new URL(urlString);
console.log(`URL: ${url.protocol}//${url.host}`);

No external module is needed to parse URLs in Deno. The URL class is available as a global, just like in your browser:

const urlString = "https://www.google.com";
const url = new URL(urlString);
console.log(`URL: ${url.protocol}//${url.host}`);

烂人 2025-01-23 14:48:11

除了使用 ECMA 脚本的本机 URL 类,如图所示通过 jsejcksn 的回答,您还可以使用 url 库来自 /std/node 兼容模块如下:

import * as UrlLib from "https://deno.land/std/node/url.ts";
const url = "https://www.google.com"
const purl = UrlLib.parse(url)
console.log(`URL: ${purl.protocol}//${purl.host}`)

输出:

URL: https://google.com

Aside from using the ECMA script's native URL class, as illustrated by jsejcksn's answer, you can also use url library from the /std/node compatibility module as follows:

import * as UrlLib from "https://deno.land/std/node/url.ts";
const url = "https://www.google.com"
const purl = UrlLib.parse(url)
console.log(`URL: ${purl.protocol}//${purl.host}`)

Output:

URL: https://google.com
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文