- Node.js Tutorial
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
- Node.js Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
NodeJS - URL Module
The NodeJS URL module provides us with utilities for the resolution and parsing of the URL. The URL string is a structured string that contains multiple segments. Following is a sample URL that has multiple segments.
The URL = "http://user:pass@site.com:80/pa/th?q=val#hash".
‘http:’ specifies the protocol segment.
‘user’ specifies the username segment.
‘pass’ specifies the password segment.
‘site.com:80’ specifies the host segment.
‘site.com’ specifies the hostname portion.
‘80’ specifies the port portion.’
‘/pa/th?q=val’ specifies the pathname segment.
‘/pa/th’ specifies the path portion.
‘?q=val’ specifies the search portion.
‘#bar’ specifies the hash segment.
Including the URL module
To include the URL module, add the below-given syntax at the beginning of your Node.js document.
Syntax
const url = require('url');
The URL module is available on the global object so that we can use it without accessing or importing it.
Now, let us see some operations using the methods in the NodeJS URL Module.
Printing the serialized URL
To print the given serialized URL, we need to use the NodeJS url.href property.
Example
console.log("The given URL -"); const myURL = new URL('https://www.tutorialspoint.com/index.htm'); console.log(myURL.href);
Output
On executing the given program, the output is displayed as follows
The given URL - https://www.tutorialspoint.com/index.htm
Printing hostname from the URL
To print the hostname segment from the URL, we use the NodeJS url.hostname property.
Example
const myURL = new URL('https://www.tutorialspoint.com:100/index.htm'); console.log("The URL: " + myURL.href); console.log("Hostname of the URL: " + myURL.hostname);
Output
Let us compile and run the program, to produce the following result
The URL: https://www.tutorialspoint.com:100/index.htm Hostname of the URL: www.tutorialspoint.com
Printing the pathname from the URL
To print the pathname segment from the URL, we use the NodeJS url.pathname property.
Example
const myURL = new URL("https://www.tutorialspoint.com/prime-pack/cloud-computing/index.asp"); console.log("The URL: " + myURL.href); console.log("Pathname of the URL: " + myURL.pathname);
Output
On executing the given program, the output is displayed as follows
The URL: https://www.tutorialspoint.com/prime-pack/cloud-computing/index.asp Pathname of the URL: /prime-pack/cloud-computing/index.asp
The URL Class
The NodeJS URLSearchParams class provides us with utility methods. Using those methods we can read and write the query segment of the URL.
List of Properties
Following is a list of properties available in the URL Class
Sr.No | Method & Description |
---|---|
1 |
Used to get and set the fragment portion of the URL. |
2 |
Used to get and set the host portion of the URL. |
3 |
Used to get and set the hostname portion of the specified URL . |
4 |
Used to get and set the serialized URL. |
5 |
Used to get the read-only serialization of the URL’s origin. |
6 | password
Used to get and set the password portion of the given URL. |
7 |
Used to get and set the pathname portion of the given URL. |
8 |
Used to set and get the port portion of the provided URL. |
9 |
Used to get and set the protocol portion of the specified URL. |
10 |
Used to get and set the serialized query portion of the URL. |
11 |
Used to get and set the username of the provided URL. |
List of Methods
Following is a list of methods available in the URL Class
Sr.No | Method & Description |
---|---|
1 |
Used to retrieve the serialized URL from the URL object. |
2 |
Used to retrieve the serialized URL from the URL object. |
3 |
Used to return the Punycode which converts and represents the domain as ASCII characters. |
4 |
Used to return the Unicode for the ASCII values present in the domain. |
5 |
Used to accept a file URL string or URL object and converts them as a properly encoded path. |
6 |
Used to return a customizable serialization of a URL string that is a representation of a WHATWG URL object |
7 |
Used to accept a path of a file and converts them into a fully-resolved file URL Object |
8 |
Used to convert a URL object into an ordinary options object that contains data every segment from the URL. |
9 |
Used to return a formatted URL string that is derived from the urlObject. |
10 |
Used to accept a URL string, parses it, and finally, it returns an object containing the segments present in the provided URL string. |
The URLSearchParams Class
The NodeJS URL class provides us with methods and properties for URL parsing and resolution. Using those, we can get and set the segments present in the URL. This class is available on the global object.
List of Methods
Following is a list of methods available in the URLSearchParams Class
Sr.No | Method & Description | |
---|---|---|
1 |
Used to create a query string object with a JSON object. |
2 |
Used to parse the input string as a query string and uses it to create a new query string object. |
3 |
Used to append a specified name/value pair as a new search parameter in the query string. |
|
4 |
Used to remove all name-value pairs whose value is name. | |
5 |
Used to returns an ES6 Iterator that allows iteration through all the name-value pairs present in the query string. |
|
6 |
Used to iterate through all the name-value pairs in the query and invokes the given function. |
|
7 |
Used to retrieve the value from a specified name in the query string. |
|
8 |
Used to get all the values of a specified name in the query string. |
|
9 |
It will prints true if the name passed to this method is present, else it prints false. |
|
10 |
Used to return an ES6 Iterator over the names of each name-value pair. |
11 |
Used to set the name-value pairs to the query string. |
12 |
Used to sort all the names of the name-value pairs in the query string. |
|
13 |
Used to retrieve a serialized string containing the search parameters of a query segment. |
|
14 |
Used to return an ES6 iterator that allows iteration through all the values of each name-value pair |
|
15 |
Used to return an ES6 Iterator that allows iteration through all the name-value pairs present in the query string. |
List of Properties
Following is the list of properties available in the urlObject
Sr.No | Method & Description |
---|---|
1 |
Used to specify the auth segment of the URL |
2 |
Used to specify the fragment segment from the URL. |
3 |
Used to specify the complete lower-cased host segment of a URL. |
4 |
Used to specify the lower-cased host name portion of the host segment of a URL. |
5 |
Used to specify the full lower-case URL string that is parsed with both protocol and host segments. |
6 |
Used to specify the path segment along with the search portion if present. |
7 |
Used to specify the pathname portion from the path segment of the URL |
8 |
Used to specify the numeric port portion of the host segment from a URL. |
9 |
Used to specify the lower-cased protocol scheme of a URL. |
10 |
Used to specify the query string portion from the URL. |
11 |
Used to specify the complete query string segment of a URL. |
12 |
It returns a Boolean value based on the two ASCII forward slashes (//) that are required after the colon (:) in the protocol segment of the URL. |