The Wayback Machine - https://web.archive.org/web/20240926030606/https://www.geeksforgeeks.org/node-js-url-pathtofileurl-api/
Open In App

Node.js URL.pathToFileURL API

Last Updated : 31 Mar, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

This URL.pathToFileURL function converts the path to a file and ensures that the URL control characters (/, \, : ) are correctly appended/adjusted when converting the given path into a File URL.

Syntax:

url.pathToFileURL(path)

Parameters: This function accepts a single parameter path that holds the path to convert a file URL. 

Return Value: This function returns the file URL object.

Example 1: In the below example, we illustrate the use of URLs.-pathToFileURL Method.

javascript




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


Output: 

URL {
  href: 'file:///D:/GeeksForGeeks',
  origin: 'null',
  protocol: 'file:',
  username: '',
  password: '',
  host: '',
  hostname: '',
  port: '',
  pathname: '/D:/GeeksForGeeks',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
}

Example 2: In the given example, we will see the use of URL.pathToFileURL.

javascript




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


Output: 

URL {
  href: 'file:///D:/NodeJS%0Aode_modules%0Apm',
  origin: 'null',
  protocol: 'file:',
  username: '',
  password: '',
  host: '',
  hostname: '',
  port: '',
  pathname: '/D:/NodeJS%0Aode_modules%0Apm',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
}

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

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


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
Node.js URL.pathname API
The url.pathname is an inbuilt application programming interface of class URL with in url module which is used to get and set the pathname portion of the URL. Syntax: const url.pathname Return value: It gets and sets the pathname portion of the URL. Below programs illustrate the use of url.pathname Method: Example 1: C/C++ Code // node program to d
1 min read
Node.js URL.format API
With the help of url.format()method, we are able to format the hostname according to our need. We have different types of other parameters which we can use to generate the hostname or to change the hostname as required. Syntax : url.format(URL[, options]) Parameter : auth is a boolean value if true then username and password have to be provided. fr
2 min read
Node.js URL.resolve(from,to) API
The url.resolve(from, to) is inbuilt method of class URL that resolves a target URL relative to a base URL. Syntax: url.resolve(from, to); Where, from: (type:String) The base URL being resolved against. to : (type:String) The "href" URL being resolved. Return value: It returns the resolved URL by given parameters in from URL by to URL(type:string).
1 min read
Node.js URL.protocol API
The url.protocol is an inbuilt application programming interface of class URL within url module which is used to get and set the protocol portion of the URL. When a URL is parsed using one of the special protocols, the url.protocol property may be changed to another special protocol but cannot be changed to a non-special protocol, and vice versa. S
2 min read
Special Schemes of Node.js URL.protocol API
The url.protocol is an inbuilt application programming interface of class URL within URL module which is used to get and set the protocol scheme of the URL. Syntax: const url.protocol Return value: It get and set protocol scheme of the URL Example 1: This example changes the special protocols to hypothetical protocols like http->https. C/C++ Cod
3 min read
Node.js url.parse(urlString, parseQueryString, slashesDenoteHost) API
The url.parse() method takes a URL string, parses it, and it will return a URL object with each part of the address as properties. Syntax: url.parse( urlString, parseQueryString, slashesDenoteHost) Parameters: This method accepts three parameters as mentioned above and described below: urlString: It holds the URL string which needs to parse.parseQu
2 min read
Node.js URL.fileURLToPath API
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 pat
1 min read
Convert relative path URL to absolute path URL using JavaScript
Given a relative URL, the task is to convert the relative URL to an absolute URL. Here, the base URL is also given. 2 approaches are discussed here, the first example has the baseURL provided by the user and the second takes it from the URL of the page. Approach 1: Get the relURL and baseURL from user.Use .split() method to split the base and relat
3 min read
What is URL Locator & what are common schemes used for the URL in HTML ?
Every HTML document on the internet has a unique URL. URL stands for Uniform Resource Locator. The URL is the HTML document's address on the internet. Any file that can be accessed via the internet has a URL, including images, JavaScript files, CSS files, Flash files, and so on. To view an HTML document in a browser, enter the URL of the document i
2 min read
How To Get URL And URL Parts In JavaScript?
In web development, working with URLs is a common task. Whether we need to extract parts of a URL or manipulate the URL for navigation, JavaScript provides multiple approaches to access and modify URL parts. we will explore different approaches to retrieve the full URL and its various components. These are the following approaches to get URL And UR
3 min read
Web API URL.search Property
The Web API URL.search property is used to get USVString which is a search string or a query string. This string contains a "?" followed by the parameters of the URL. Syntax: var str = URL.search Return Value: This property returns a USVString search string. Example 1: C/C++ Code <!DOCTYPE html> <html> <body> <h1>GeeksforGee
1 min read
Web API URL.protocol Property
The Web API URL.protocol property is used to get USVString containing the protocol scheme of the URL. Syntax: var str = URL.protocol Return Value: This property returns a USVString containing the protocol scheme of the URL. Example 1: C/C++ Code <!DOCTYPE html> <html> <body> <h1>GeeksforGeeks</h1> <button onclick=
1 min read
Web API URL.port Property
The Web API URL.port property is used to get USVString containing the port number of the URL. Syntax: var str = URL.port Return Value: This property returns a USVString containing the port number of the URL. Example 1: C/C++ Code <!DOCTYPE html> <html> <body> <h1>GeeksforGeeks</h1> <button onclick="get()"
1 min read
Web API URL.pathname Property
The Web API URL.pathname property is used to get USVString containing an initial “/” followed by the path of the URL. Syntax: var str = URL.pathname Return Value: This property returns a USVString containing the pathname of the URL. Note: If there is no path set for the URL, so it will Return only /. Example 1: There is no path set for the URL, so
1 min read
Web API URL.username Property
The Web API URL.username property returns a USVString containing the username specified before the domain name of the URL Syntax: var str = URL.username Return Value: This property returns a USVString containing the username specified before the domain name of the URL. Example 1: C/C++ Code <!DOCTYPE html> <html> <body> <h1>
1 min read
Web API URL.password Property
The Web API URL.password property returns a USVString containing the password specified before the domain name of the URL. A USVString is a sequence of Unicode scalar values. Syntax: var str = URL.password Return Value: This property returns a USVString containing the password specified before the domain name of the URL. Example 1: C/C++ Code <!
1 min read
Web API URL.origin Property
The Web API URL.origin property returns a USVString containing the origin of the URL. Syntax: var str = URL.origin Return Value: This property returns a USVString containing the origin of the URL. Example 1: C/C++ Code <!DOCTYPE html> <html> <body> <h1>GeeksforGeeks</h1> <div id="abc"></div> <b
1 min read
Web API URL
The Web API URL is used to access and manipulate URLs. URL stands for Uniform Resource Locator. A URL is a unique address which is pointing to the resource. The URL is essential for making HTTP requests for making interactions with the API and for getting or sending data. Table of Content Concepts and UsageAccessing components of URL URL Modificati
4 min read
Node.js URL.domainToUnicode
The url.domainToUnicode is an inbuilt application programming interface of class URL with in url module. It returns the Unicode serialization of the domain. If the domain is invalid, the empty string is returned. Syntax : const url.domainToASCII Domain value : string Return value : string Example : const url = require('url'); console.log(url.domain
1 min read
Node.js URL.domainToASCII
The url.host is an inbuilt application programming interface of class URL with in url module. It returns the Punycode ASCII serialization of the domain. If domain is an invalid domain, the empty string is returned. Syntax : const url.domainToASCII Domain value : string Return value : string Example : const url = require('url'); console.log(url.doma
1 min read