- Node.js Tutorial
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
- Node.js Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
NodeJS - url.pathToFileURL() Method
The NodeJS url.pathToFileURL() method of the class URL accepts a path of a file and converts them into a fully-resolved file URL Object.
This method will make sure that the file path is resolved absolutely. If the file path contains URL control characters, they will be encoded and converted into a fully-resolved File URL.
Syntax
Following is the syntax of the NodeJS url.pathToFileURL() method of URL class
url.pathToFileURL(path)
Parameters
path: This parameter specifies a path that will be converted to a File URL.
Return value
This method returns the file URL object.
Example
If we pass a file path to the NodeJS url.pathToFileURL() method, it will convert that path into a file URL object.
In the following example, we are passing the ‘__filename’ (i.e. it gets the path of the current working file) to the pathToFileURL() method.
const { pathToFileURL } = require('node:url'); let PtoF = pathToFileURL(__filename); console.log(PtoF);
Output
On executing the above program, it will generate the following output
URL { href: 'file:///C:/Users/Lenovo/Desktop/JavaScript/nodefile.js', origin: 'null', protocol: 'file:', username: '', password: '', host: '', hostname: '', port: '', pathname: '/C:/Users/Lenovo/Desktop/JavaScript/nodefile.js', search: '', searchParams: URLSearchParams {}, hash: '' }
Example
If the path we pass to the pathToFileURL() method contains URL control characters, it will encode and convert them into a fully-resolved file URL object.
In the following example, we are passing a path to the pathToFileURL() method that contains URL control characters.
const { pathToFileURL } = require('node:url'); let PtoF = pathToFileURL('/footer#/file%.js'); console.log(PtoF);
Output
On executing the above program, it will generate the following output
URL { href: 'file:///C:/footer%23/file%25.js', origin: 'null', protocol: 'file:', username: '', password: '', host: '', hostname: '', port: '', pathname: '/C:/footer%23/file%25.js', search: '', searchParams: URLSearchParams {}, hash: '' }
To Continue Learning Please Login
Login with Google