How do you parse a URL in deno like node.js
url.parse()
?
Asked
Active
Viewed 692 times
2 Answers
6
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}`);

jsejcksn
- 27,667
- 4
- 38
- 62
-
Thanks for the reminder. My JS activity dates way back to well before Firefox 1.0 and sometimes my brain does not quickly absorb all the new stuff that shows up in ECMA Script over time =) – Timothy C. Quinn Mar 24 '22 at 16:25
-
@TimothyC.Quinn No problem. (Also note that [the same has been true in Node.js since version 10](https://nodejs.org/api/globals.html#url).) – jsejcksn Mar 24 '22 at 18:25
0
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

Timothy C. Quinn
- 3,739
- 1
- 35
- 47