0
0
Node.jsframework~5 mins

path.basename and path.dirname in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does path.basename do in Node.js?

path.basename returns the last part of a file path, usually the file name with extension.

Click to reveal answer
beginner
What is the purpose of path.dirname in Node.js?

path.dirname returns the directory part of a file path, which is everything except the last part.

Click to reveal answer
beginner
Given the path /home/user/docs/file.txt, what will path.basename return?

It will return file.txt, the last part of the path.

Click to reveal answer
beginner
Given the path /home/user/docs/file.txt, what will path.dirname return?

It will return /home/user/docs, the directory part of the path.

Click to reveal answer
intermediate
How can you use path.basename to get the file name without its extension?

You can pass a second argument with the extension to remove, like path.basename('/path/file.txt', '.txt') which returns file.

Click to reveal answer
What does path.basename('/folder/file.txt') return?
Afile.txt
B/folder/file.txt
C/folder
Dfile
What does path.dirname('/folder/file.txt') return?
Afile
Bfile.txt
C/folder/file.txt
D/folder
How do you get the file name without extension using path.basename?
AUse <code>path.extname</code>
BUse <code>path.dirname</code>
CPass the extension as second argument to remove it
DUse <code>path.join</code>
Which module do you need to use basename and dirname?
Apath
Bhttp
Cos
Dfs
What will path.basename('/folder/file.txt', '.txt') return?
Afile.txt
Bfile
C.txt
D/folder/file
Explain how path.basename and path.dirname help you work with file paths in Node.js.
Think about splitting a full file path into folder and file name.
You got /4 concepts.
    Describe a real-life situation where you might use path.basename and path.dirname in a Node.js project.
    Imagine you are building a file manager or uploader.
    You got /4 concepts.