Skip to content

Latest commit

 

History

History
41 lines (27 loc) · 1.67 KB

File metadata and controls

41 lines (27 loc) · 1.67 KB

🐢 Node.js

🌟 The different core modules

URL (API WHATWG)

Browser URL API implementation. By default available on the Global namespace as for timers. URL is really useful for creating and manipulating URLs (like for path, many inexperienced developers will build homemade solutions when this API would allow them to do what they want very simply).

To my taste, it should be used much more (even to declare and identify a URL in JavaScript code). The evolution and maintenance of codes will be much simpler.

const myURL = new URL("https://example.org/?abc=123");
console.log(myURL.searchParams.get("abc"));
// Prints 123

myURL.searchParams.append("abc", "xyz");
console.log(myURF.href);
// Prints https://example.org/?abc=132&abc=xyz

myURL.searchParams.delete("abc");
myURL.searchParams.set("a", "b");
console.log(myURL.href);
// Prints https://example.org/?a=b

The constructor will take care of validating the URL for you (an error will be thrown if the URL is not valid). You can then retrieve all the information you want on your URL (protocol, username, password, hostname, port, pathname, query etc).

URL can also be used in ESM with import.meta.url to replace __dirname.

import { readFileSync} from "fs";

const buffer = readFileSync(new URL("./data.proto", import.meta.url));

Bonus: How to migrate from querystring to URLSearchParams in Node.js


⬅️ 🌟 The different core modules: Timers | ➡️ 🌟 The different core modules: OS