VOOZH about

URL: https://www.geeksforgeeks.org/node-js/node-js-url-pathtofileurl-api/

⇱ Node.js URL.pathToFileURL API - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Node.js URL.pathToFileURL API

Last Updated : 31 Mar, 2023

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.

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.

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

Comment
Article Tags:

Explore