📜  Node.js URL.href API(1)

📅  最后修改于: 2023-12-03 14:44:40.701000             🧑  作者: Mango

Node.js URL.href API

The URL.href API in Node.js is used to get or set the complete URL string of a parsed URL object. It returns a string containing the complete URL, including the protocol, domain, path, query parameters, and fragment identifier.

Syntax
urlObject.href
Parameters

None.

Return Value

A string containing the complete URL.

Example
const url = require('url');

// Creating a URL object
const myUrl = new URL('https://www.example.com/users?id=123');

// Getting the complete URL
console.log(myUrl.href);
// Expected output: https://www.example.com/users?id=123

In the above example, we have created a URL object using the new URL() method and passed the URL string 'https://www.example.com/users?id=123' as a parameter. We have then printed the complete URL using the href property of the URL object.

Conclusion

In this tutorial, we have learned about the URL.href API in Node.js, which is used to get or set the complete URL string of a parsed URL object. By using this API, we can easily retrieve or manipulate the URL.