Node.js File System Module #52 Flashcards
How would you require access and interaction with the file system
const fs = require(‘fs’)
Memorize at least some of the fs methods or refer back to this later.
- fs.access( ): check if the file exists and Node can access it with its permissions
- fs.appendFile( ): append data to a file. If the file does not exist, it’s created
- fs.chmod( ): change the permissions of a file specified by the filename passed. Related: fs.lchmod( ), fs.fchmod()
- fs.chown( ): change the owner and group of a file specified by the filename passed.
Related: fs.fchown( ),fs.lchown( )
fs.close( ): close a file descriptor
fs.copyFile( ): copies a file
fs.createReadStream( ): create a readable file stream
fs.createWriteStream( ): create a writable file stream
fs.link( ): create a new hard link to a file
fs.mkdir( ): create a new folder
fs.mkdtemp( ): create a temporary directory
fs.open( ): set the file mode
fs.readdir( ): read the contents of a directory
fs.readFile( ): read the content of a file.
• Related: fs.read( )
fs.readlink( ): read the value of a symbolic link
fs.realpath( ): resolve relative file path pointers (., ..) to the full path
fs.rename( ): rename a file or folder
fs.rmdir( ): remove a folder
fs.stat( ): returns the status of the file identified by the filename passed. Related: fs.fstat( ), fs.lstat( )
fs.symlink( ): create a new symbolic link to a file
fs.truncate( ): truncate to the specified length the file identified by the filename passed. Related: fs.ftruncate( )
fs.unlink( ): remove a file or a symbolic link
fs.unwatchFile( ): stop watching for changes on a file
fs.utimes( ): change the timestamp of the file identified by the filename passed. Related: fs.futimes()
fs.watchFile( ): start watching for changes on a file. Related: fs.watch( )
fs.writeFile( ): write data to a file. Related: fs.write( )
The full list of methods that can be used under the fs module are asynchronous by default. How would you make them synchronous if needed ?
Append Sync to the method. Example:
For example:
fs. rename( ) fs. renameSync( ) fs. write( ) fs. writeSync( )
How can you access the “path” module in Node.js?
Require it as below:
const path = require(‘path’)
What is path.sep?
It’s simply the path segment separator /
/ Mac/Linux
\ Windows
What does the path.basename( ) method do?
Return the last portion of a path. A second parameter can filter out the file extension:
require(‘path’).basename(‘/test/something’) //something
require(‘path’).basename(‘/test/something.txt’) //something.txt
require(‘path’).basename(‘/test/something.txt’, ‘.txt’) //something
What does the path.dirname( ) method do?
Return the directory part of a path:
require(‘path’).dirname(‘/test/something’) // /test
require(‘path’).dirname(‘/test/something/file.txt’) // /test/something
What does the path.extname( ) method do?
Return the extension part of a path
require(‘path’).extname(‘/test/something’) // ‘’
require(‘path’).extname(‘/test/something/file.txt’) // ‘.txt’
What does the path.isAbsolute( ) method do?
Returns true if it’s an absolute path
require(‘path’).isAbsolute(‘/test/something’) // true
require(‘path’).isAbsolute(‘./test/something’) // false
What does the path.join( ) method do?
Joins two or more parts of a path:
const name = 'flavio' require('path').join('/', 'users', name, 'notes.txt') //'/users/flavio/notes.txt'
What does the path.normalize( ) method do?
Tries to calculate the actual path when it contains relative specifiers like . or .., or double slashes:
require(‘path’).normalize(‘/users/flavio/..//test.txt’) ///users/test.txt
What does the path.parse( ) method do?
Parses a path to an object with the segments that compose it:
root: the root dir: the folder path starting from the root base: the file name + extension name: the file name ext: the file extension
Example:
require(‘path’).parse(‘/users/test.txt’)
results in
{ root: '/', dir: '/users', base: 'test.txt', ext: '.txt', name: 'test' }
What does the path.relative( ) method do?
Accepts 2 paths as arguments. Returns the relative path from the first path to the second, based on the current working directory.
Example:
require(‘path’).relative(‘/Users/flavio’, ‘/Users/flavio/test.txt’) //’test.txt’
require(‘path’).relative(‘/Users/flavio’, ‘/Users/flavio/something/test.txt’) //’something/test.txt’
What does the path.resolve( ) method do?
You can get the absolute path calculation of a relative path using path.resolve():
path.resolve(‘flavio.txt’) //’/Users/flavio/flavio.txt’ if run from my home folder
By specifying a second parameter, resolve will use the first as a base for the second:
path.resolve(‘tmp’, ‘flavio.txt’)//’/Users/flavio/tmp/flavio.txt’ if run from my home folder
If the first parameter starts with a slash, that means it’s an absolute path:
path.resolve(‘/etc’, ‘flavio.txt’)//’/etc/flavio.txt’
What is the main use for the ‘OS’ module? And like other modules, how is it accessed?
This module provides many functions that you can use to retrieve information from the underlying operating system and the computer the program runs on, and interact with it.
REQUIRE const os = require('os')