Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Url Module in NodeJs

Url Module in NodeJs

The url module in Node.js is used to parse and manipulate URLs. It comes built-in, so you don’t need to install anything.


πŸ”§ How to Use the URL Module

βœ… Importing the module

js

const url = require('url');


πŸ“¦ Example: Parsing a URL

js

const url = require('url');const myURL = 'https://example.com:8080/path/name?query=123#section';const parsed = url.parse(myURL, true); // `true` parses query string into an objectconsole.log(parsed.hostname); // example.comconsole.log(parsed.pathname); // /path/nameconsole.log(parsed.port); // 8080console.log(parsed.query); // { query: '123' }console.log(parsed.hash); // #section


🌐 Newer Way (WHATWG URL API)

Node.js also supports the newer URL class (preferred for newer code):

js

const { URL } = require('url');const myURL = new URL('https://example.com:8080/path/name?query=123#section');console.log(myURL.hostname); // example.comconsole.log(myURL.pathname); // /path/nameconsole.log(myURL.port); // 8080console.log(myURL.searchParams.get('query')); // 123console.log(myURL.hash); // #section


πŸ”„ Modifying URLs

js

myURL.pathname = '/newpath';myURL.searchParams.set('page', '2');console.log(myURL.href); // https://example.com:8080/newpath?query=123&page=2#section


🧠 Use Cases

  • Parse incoming request URLs in a web server.

  • Modify and build query strings.

  • Redirect URLs dynamically.

  • Extract data from URL parameters.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql