Open In App

Node.js URL.fileURLToPath API

Last Updated : 14 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

This URL.fileURLToPath function decodes the file URL to a path string and ensures that the URL control characters (/, %) are correctly appended/adjusted when converting the given file URL into a path.

Syntax:

url.fileURLToPath( url )

Parameters: This function accepts single parameter url which holds the file URL string or object to convert into path.

Return Value: It returns a string which represents the fully-resolved platform-specific file path.

Below programs illustrates the use of URL.fileURLToPath() Method in Node.js:

Example 1:




// Node program to demonstrate the 
// URL.fileURLToPath() API as Setter
  
// Importing the module 'url' 
const url = require('url');
  
 // Some random path from system
  
// Converting our file to properly encoded path                    
console.log(url.fileURLToPath(file)) 


Output:

\\computerscience\geeksforgeeks.txt

Example 2:




// Node program to demonstrate the 
// URL.fileURLToPath() API as Setter
  
// Importing the module 'url' 
const url = require('url');
  
// Some random path from system
   
// Converting the file to properly encoded path
console.log(url.fileURLToPath(file))


Output:

 C:\path\example\gfg 

Note: The above program will compile and run by using the node app.js command.

Reference: https://nodejs.org/api/url.html#url_url_fileurltopath_url


Previous Article
Next Article

Similar Reads

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 c
4 min read
Node.js URL.hash API
The url.hash is an inbuilt application programming interface of class URL within url module which is used to get and set the fragment portion of the URL. Syntax: url.hash Return value: It gets and sets the fragment portion of the URL. Below programs illustrate the use of url.hash Method: Example 1: C/C++ Code // node program to demonstrate the // u
1 min read
Node.js URL.host API
The url.host is an inbuilt application programming interface of class URL with in url module which is used to get and set the host portion of the URL. Syntax: const url.host Return value: It gets and sets the host portion of the URL.Below programs illustrates the use of url.host Method:Example 1: C/C++ Code // node program to demonstrate the // url
1 min read
Node.js URL.hostname API
The url.hostname is an inbuilt application programming interface of class URL with in url module which is used to get and set the hostname portion of the URL. The key difference between url.host and url.hostname is that url.hostname does not include the port. Syntax: const url.hostname Return value: It gets and sets the hostname portion of the URL.
1 min read
Node.js URL.href API
The url.href is an inbuilt application programming interface of class URL with in the url module which Gets and sets the serialized URL. Getting the value of the href property is equivalent to calling the url.toString() method.Setting the value of this property to a new value is equivalent to creating a new URL object using new URL(value). Each of
2 min read
Node.js URL.password API
The url.password is an inbuilt application programming interface of class URL with in url module which is used to get and set the password portion of the URL. Syntax: const url.password Return value: It gets and sets the password portion of the URL.Below programs illustrate the use of url.password Method:Example 1: C/C++ Code // node program to dem
2 min read
Node.js URL.origin API
url.origin is an inbuilt application programming interface(API) of the URL class within the url module. url.origin API is used to gets the read-only serialization of the URL’s origin. Syntax: url.origin url : It is an object created by URL constructor. Example 1: C/C++ Code //Importing the url module const url = require('url'); //Creating an URL_1
1 min read
Node.js URL.username API
URL.username is an inbuilt application programming interface(API) of the URL class within Node.JS. URL.username API is used to get and set the username of the URL. Syntax: url.username URL: It is an object created by a URL constructor. Example 1: (Getting the username of URL) C/C++ Code //Creating an URL_1 object with URL constructor. const URL_1 =
1 min read
Node.js URL.search API
URL.search is an inbuilt application programming interface(API) of the URL class within the Node.JS. URL.search API is used to get and set the query part of URL. Syntax: url.search url : It is an object created by URL constructor. Example 1: (Getting query string of the URL) C/C++ Code //Creating an URL_1 object with URL constructor. const URL_1 =
1 min read
Node.js URL.port API
The url.port is an inbuilt application programming interface of class URL within url module which is used to get and set the port portion of the URL.the port value may be a number or a string containing a number in the range 0 to 65535 (inclusive). Setting the value to the default port of the URL objects given protocol will result in the port value
3 min read