# url

***

**1. Parsing a URL**

```js
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**

```js
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**

```js
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**

```js
const url = require('url');

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

myUrl.host = 'www.example.org';

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

**5. Changing the pathname of a URL**

```js
const url = require('url');

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

myUrl.pathname = '/new/path/to/file';

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

**6. Changing the search query of a URL**

```js
const url = require('url');

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

myUrl.search = '?newQuery=string';

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

**7. Changing the fragment of a URL**

```js
const url = require('url');

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

myUrl.fragment = 'newFragment';

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

**8. Creating a new URL object**

```js
const url = require('url');

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

**9. Creating a URL object from a string**

```js
const url = require('url');

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

**10. Getting the URL search parameters**

```js
const url = require('url');

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

const searchParams = myUrl.searchParams;

console.log(searchParams.get('query')); // 'string'
```

**11. Setting the URL search parameters**

```js
const url = require('url');

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

myUrl.searchParams.set('query', 'newString');

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

**12. Deleting a URL search parameter**

```js
const url = require('url');

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

myUrl.searchParams.delete('query

```
