Node.js urlObject.href API
Last Updated :
31 Mar, 2023
Improve
The urlObject.href API is used to return the complete URL string along with the protocols(HTTP) and pathname or other search terms.
Syntax:
urlObject.href
For example: 'http://www.geeksforgeeks.com/login/password.html' Here, Protocol = http Path = /login Host = 'www' File Name = password.html
Example 1: The below example illustrates the use of urlObejct.href method in Node.js.
// Node program to demonstrate the
// urlObject.href API as Setter
// It will return a URL object
const gfg = new URL('https://www.geeksforgeeks.com/login.html ');
// Output the fetched url
console.log(gfg.href);
Output:
https://www.geeksforgeeks.com/login.html
Example 2: In this example, we will see the use of urlObject.href
// Node program to demonstrate the
// url.href API as Setter
// Importing the module 'url-parse'
// Use command 'npm install url-parse' in command
// prompt to import this module
const parse = require('url-parse');
const url = parse('https://www.example.com:777/a/b?c=d&e=f#g ');
console.log(url.href);
Output:
https://www.example.com:777/a/b?c=d&e=f#g
Note: The above program will compile and run by using the node app.js command.
Reference: https://nodejs.org/api/url.html#url_urlobject_href