Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

NodeJS URL Modules

URL Modules

Url module is one of the core modules in NodeJS, which provides utilities for URL resolution and parsing. To include the URL module, use the require() method:-

example
var url = require('url');

The NodeJS URL module is one of the very important modules, which provides various properties to deal with URL, which includes the following:-

URL Module PropertiesDescription
hrefIt provides the complete URL string.
protocolIt is used to gets and sets the protocol portion of the URL.
hostIt returns the hostname and port number.
hostnameIt returns the lower-cased hostname portion of the host component.
portIt returns the port number specified in the URL.
pathnameIt is used to gets and sets the path portion of the URL.
searchIt is used to gets and sets the serialized query portion of the URL.
queryIt returns either the params portion of the query string, or a querystring-parsed object.
hashIt is used to gets and sets the fragment portion of the URL.
Key Takeaways
  • The URL class (WHATWG URL API) is the modern way to parse and construct URLs in Node.js.
  • new URL(urlString) parses a URL into its components: protocol, hostname, pathname, search, hash.
  • URLSearchParams makes it easy to read and modify query string parameters.
  • The legacy url.parse() is deprecated - use the WHATWG URL API instead.
  • url.resolve() is also deprecated - use new URL(path, base) for URL resolution.
  • Always encode user input in URLs using encodeURIComponent() to prevent injection attacks.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.