Node.js URL.format(urlObject) API
The URL.format(urlObject) is the inbuilt API provided by URL class, which takes an object or string and return a formatted string derived from that object or string.
Syntax:
const url.format(urlObject)
If the urlObject is not an object or string, then it will throw a TypeError.
Return value: It returns string derived from urlObject.
The urlObject can have the following fields or keys:
- protocol
- slashes
- auth
- hostname
- host
- port
- pathname
- search
- query
- hash
The formatting process is as follows:
- If urlObject.protocol is a string it is appended to the result else if not undefined and not a string then Error is thrown.
- If urlObject.protocol do not end with ASCII colon ( : ) then, the literal ‘:’ is appended to the result.
- If either of the following property is true, then literals ‘//’ are appended to the result:
- urlObject.slashaes is true.
- urlObject.protocol is http, https, ftp, gopher, or file, then slashes will be automatically true even if slashes is false.
- If the urlObject.auth is not undefined and urlObject.host or urlObject.hostname is also not undefined then auth is appended to the result with literal ‘@’ irrespective of whether the literal ‘@’ present or not at the end.
- If urlObject.host is a string it is appended to the result else if not undefined and not a string then Error is thrown.
- If it is undefined then urlObject.hostname is considered.
- If urlObject.hostname is a string it is appended to the result else if not undefined and not a string then Error is thrown.
- If both host and hostname are defined then host will be given considered.
- If hostname is considered and urlObject.port is defined then literal ‘:’ will be appended to the result along with urlObject.port.
- If urlObject.pathname is a string but not empty string and not starting with literal ‘/’, then literal ‘/’ is appended to the result.
- urlObject.pathname is appended to the result.
- Else UrlObject.pathname is not a string then Error is thrown.
- If urlObject.search is a string but not empty string and not starting with literal ‘?’, then literal ‘?’ is appended to the result.
- urlObject.search is appended to the result.
- If urlObject.search is not a string then Error is thrown.
- If urlObject.query is an Object then literal ‘?’ is appended to the result along with output of calling the querystring module’s stringify() method passing the value of urlObject.query.
- If both urlObject.search and urlObject.query are defined then urlObject.search will only be considered.
- If urlObject.hash is a string but not empty string and not starting with literal ‘#’, then literal ‘#’ is appended to the result.
- urlObject.hash is appended to the result.
- Else urlObject.hash is not a string and is not undefined then Error is thrown.
-
1. Initially, an empty string (‘’ say result) is created and then following parameters are looked for in order.
2. urlObject.protocol: string
-
3. urlObject.slashes: boolean
-
4. urlObject.auth: string
-
5. urlObject.host: string
-
6. urlObject.hostname: string
-
7. urlObject.port: (number | string)
-
8. urlObject.pathname: string
-
9. urlObject.search: string
-
10. urlObject.query: Object
-
11. urlObject.hash: string
-
12. Finally, the result is returned.
Example 1
/* node program to demonstrate the URL.format API. */ //importing the module 'url' const url = require( 'url' ); //creating and initializing urlObject var urlObject={ protocol: 'https' , hostname: 'example.com' , port: 1800, pathname: 'sample/path' , query: { page: 1, format: 'json' }, hash: 'first' } //getting the derived URL from urlObject using the url.format function var sampleUrl=url.format(urlObject); //Display the returned value console.log(sampleUrl.toString()); |
Output: https://example.com:1800/sample/path?page=1&format=json#first
Example 2
/* node program to demonstrate the URL.format API. */ //importing the module 'url' const url = require( 'url' ); //creating and initializing urlObject var urlObject={ protocol: 'prct' , slashes: false , host: 'example.com' , auth: 'abc' , pathname: '/sample/path' , search: 'something' , hash: 'first' } //getting the derived URL from urlObject using the url.format function var sampleUrl=url.format(urlObject); //Display the returned value console.log(sampleUrl.toString()); |
Output: prct:abc@example.com/sample/path?something#first
NOTE: The above program will compile and run by using the node fileName.js command.
Reference:
https://nodejs.org/api/url.html#url_url_format_urlobject