url


1. Parsing a URL

const url = require('url');

const parsedUrl = url.parse('https://example.com:8080/path/to/file?query=string#fragment');

console.log(parsedUrl.protocol); // 'https:'
console.log(parsedUrl.host); // 'example.com:8080'
console.log(parsedUrl.pathname); // '/path/to/file'
console.log(parsedUrl.query); // 'query=string'
console.log(parsedUrl.fragment); // 'fragment'

2. Converting a URL to a string

const url = require('url');

const myUrl = new URL('https://example.com:8080/path/to/file?query=string#fragment');

console.log(myUrl.toString()); // 'https://example.com:8080/path/to/file?query=string#fragment'

3. Changing the protocol of a URL

const url = require('url');

const myUrl = new URL('https://example.com:8080/path/to/file?query=string#fragment');

myUrl.protocol = 'http:';

console.log(myUrl.toString()); // 'http://example.com:8080/path/to/file?query=string#fragment'

4. Changing the host of a URL

5. Changing the pathname of a URL

6. Changing the search query of a URL

7. Changing the fragment of a URL

8. Creating a new URL object

9. Creating a URL object from a string

10. Getting the URL search parameters

11. Setting the URL search parameters

12. Deleting a URL search parameter