How To Parse URL in Deno

800 views Asked by At

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

2

There are 2 answers

2
jsejcksn On BEST ANSWER

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}`);

0
Timothy C. Quinn On

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