Node Js Parse File Extension or Name from URL Tutorial

In this comprehensive guide, you will learn how to profoundly get the full file name using the file path or URL in the node js application.

To extract the file name and file extension in node js, we will use the default Node js path module.

We will not just be learning to pull out the file name but also learn to obtain file extension or simple file name without file extension in node js.

In node, the path module offers handy methods for working with file and directory paths. To access the methods of the path, you have to import the path module in the file where you work with file and directory paths.

In this guide, we will use the basename() and extname() methods to deal with files.

The path.basename() method returns the last piece of a path, alike to the Unix basename command.

The path.extname() method returns the extension of the path, from the last occurrence of the . (period) character to end of string in the last portion of the path.

How to Get File Name or Extension from URL in Node Js with Path Module

  • Step 1: Build New Folder
  • Step 2: Set Up Package Json
  • Step 3: Create and Register App File
  • Step 4: Retrieve File Name / Extension
  • Step 5: Run Node Project

Build New Folder

We are going to use the mkdir command to make a new directory for node js app.

mkdir node-app

You can now cd into the project folder.

cd node-app

Set Up Package Json

You are basic app folder is ready now you can run the npm initializer command to generate a package file.

You have to invoke the given command.

npm init

Now, you can see the project information in your package.json file.

Create and Register App File

Let’s make new server.js file in your node project, this file will be the home for file handling code.

Ensure that you added the command in the scripts array, this will allow you to execute the server.js script from terminal’s command line interface.

Now, open the package.json file and add the code into the file.

...
...
...
"scripts": {
   "start": "node server.js"
},
...
...
...

Retrieve File Name / Extension

Add the path module at the topmost part to get access to its methods for pulling out the file name and extension.

Keep the two files in your node project; likewise, we have declared the file path.

We will use the basename and extname to get the file name and extension with these paths.

Add code inside the server.js file.

const path = require('path')

// File url
URL = './package-lock.json'

let fileName_1 = path.basename(URL)
let fileExtension_1 = path.extname(URL)
let noExt_1 = path.basename(fileName_1, fileExtension_1)

console.log(
  'Full filename:',
  fileName_1,
  '\n',
  'File extension:',
  fileExtension_1,
  '\n',
  'Just file name:',
  noExt_1,
)

// Extract file name
File_PATH = './demo.txt'

let fileName_2 = path.basename(File_PATH)
let fileExtension_2 = path.extname(File_PATH)
let noExt_2 = path.basename(fileName_2, fileExtension_2)
console.log(
  'Full filename:',
  fileName_2,
  '\n',
  'File extension:',
  fileExtension_2,
  '\n',
  'Just file name:',
  noExt_2,
)

Run Node Project

We can now use the following command to execute the node script to display the output on the terminal.

node server.js

On your console you can now see the result.

Node Js Parse File Extension or Name from URL Tutorial

Conclusion

In this tutorial, we shed light on the valuable functionality that is to get the file extension in node js.

Not just that we also covered how to get the full file name with extension and file name without extension using the Path module. We also discussed about the basename and extname methods of the path module.

The path module is not limited to this; rather, you can use it to add more value while working with files and directories.

You can check out more about path module here.