Node.js URL.format(urlObject) API - GeeksforGeeks

14 Oct, 2021
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 derieved 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:
1. Initially, an empty string (‘’ say result) is created and then following parameters are looked for in order.
2. urlObject.protocol: stringIf 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.
2. urlObject.protocol: string
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.
3. urlObject.slashes: booleanIf 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 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.
urlObject.slashaes is true.
urlObject.protocol is http, https, ftp, gopher, or file, then slashes will be automatically true even if slashes is false.
4. urlObject.auth: stringIf 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 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.
5. urlObject.host: stringIf 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.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.
6. urlObject.hostname: stringIf 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 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.
7. urlObject.port: (number | string)If hostname is considered and urlObject.port is defined then literal ‘:’ will be appended to the result along with urlObject.port.
If hostname is considered and urlObject.port is defined then literal ‘:’ will be appended to the result along with urlObject.port.
8. urlObject.pathname: stringIf 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.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.
9. urlObject.search: stringIf 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.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.
10. urlObject.query: ObjectIf 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.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.
11. urlObject.hash: stringIf 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.
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.
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 urlObjectvar urlObject={        protocol: 'https',        hostname: 'example.com',        port: 1800,        pathname: 'sample/path',        query: {                page: 1,                format: 'json'        },        hash: 'first'    }  //getting the derieved URL from urlObject using the url.format functionvar sampleUrl=url.format(urlObject);  //Display the returned valueconsole.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 urlObjectvar urlObject={        protocol: 'prct',        slashes: false,        host: 'example.com',        auth: 'abc',        pathname: '/sample/path',        search: 'something',        hash: 'first'    }  //getting the derieved URL from urlObject using the url.format functionvar sampleUrl=url.format(urlObject);  //Display the returned valueconsole.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
Node-URL
Picked
Node.js
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Node.js fs.writeFile() Method
Difference between promise and async await in Node.js
How to use an ES6 import in Node.js?
Express.js res.sendFile() Function
How to read and write Excel file in Node.js ?
Mongoose | findByIdAndUpdate() Function
Node.js fs.readdirSync() Method
Express.js res.send() Function
What are the differences between npm and npx ?
Node.js fs.readdir() Method