How to Parse File Name and Extension from URL in Node
In this step by step tutorial, we will learn how to get file extension, full file name, file extension using file path in node js application using the third-party package.
While working on a web development project, you deal with many situations where you meet several peculiar requirements.
We are trying to bring your attention to such a topic that deals with files, file names, and extensions in the node js environment.
In our web development career, we seldom have to grab file names, files path and files extensions based on the file path or file URL.
To retrieve the file name and extension, we will use the node’s default path module. The path module is one of the best and most handy module that helps you manage the file
The path module is a relentless module that offers eloquent utilities for working with file and directory paths.
It comes by default with node, which means you don’t have to install it manually.
To take the utter benefit of the path module you have to import inside the node file. Let’s checkout the guide step by step to build the feature.
Node Js Extract File Name and File Extension from URL Example
- Step 1: Make Blank Directory
- Step 2: Run NPM Init
- Step 3: Build Server File
- Step 4: Get File Extension and Name
- Step 5: Display Output
Build New Folder
We will make a blank app folder for holding the node related files and folders.
mkdir node-app
After creating the file you have to enter into the project.
cd node-app
Run NPM Init
Again on the command prompt type the provided command thereafter execute the command this will create the package.json file.
npm init
Create and Register App File
You have to now make the app.js file similarly register the file name into the scripts array in package.json file.
...
...
...
...
"scripts": {
"start": "node app.js"
},
...
...
...
...
Retrieve File Name / Extension
Following code example shows file name and extension of two files that are located in our node project, we are displaying the output on the console using console.log() method.
Update code within the app.js file.
const path = require('path')
FilePath = './index.html'
let fullFileInfo_1 = path.basename(FilePath)
let extension_1 = path.extname(FilePath)
let onlyFilename_1 = path.basename(fullFileInfo_1, extension_1)
console.log(
'\n',
'File name:', onlyFilename_1,
'\n',
'File extension:', extension_1,
'\n',
'Complete filename:', fullFileInfo_1,
)
// Get Filename
File_PATH = './tiny.pdf'
let fullFileInfo_2 = path.basename(File_PATH)
let extension_2 = path.extname(File_PATH)
let onlyFilename_2 = path.basename(fullFileInfo_2, extension_2)
console.log(
'\n',
'File name:', onlyFilename_2,
'\n',
'File extension:', extension_2,
'\n',
'Complete filename:', fullFileInfo_2,
'\n',
)
Display Output
In the last step, you just have to execute the given command.
node app.js
You can see the file information on the terminal screen as given below.
Conclusion
In this short guide, we have demonstrated how to identify the full filename, file extension and only file name using path package of node js.
We learned how to use the path module and use its various methods and properties to pull out file information in node js.