1. The basic API node
1.1 the fs module
1.1.1 the fs. WriteFile ()
Fs.writefile () By default, this API replaces the contents of the file if it already exists. You can modify the default behavior by specifying flags: fs.writefile ('/Users/ Joe /test.txt', content, {flag: 'a+'}, err => {})Copy the code
Flags that may be used are:
- R + : opens a file for reading and writing.
- W + : Opens the file for reading and writing, positioning the stream to the beginning of the file. Create a file if it does not exist.
- A Opens the file for writing, positioning the stream to the end of the file. Create a file if it does not exist.
- A + opens the file for reading and writing, positioning the stream to the end of the file. Create a file if it does not exist.
1.1.2 fs. AppendFile ()
Appends the content to the end of the fileCopy the code
1.1.3 fs. The access ()
Use fs.access() to check whether the folder exists and whether Node.js has access rights.Copy the code
1.1.4 fs. The mkdir () or fs. MkdirSync ()
You can create new folders using fs.mkdir() or fs.mkdirsync ().Copy the code
try { if (! Fs.existssync (folderName)) {//existsSync checks for folder fs.mkdirsync (folderName) // create}} Catch (err) {console.error(err)}Copy the code
1.1.5 fs. Readdir () or fs. ReaddirSync ()
You can read the contents of a directory using fs.readdir() or fs.readdirsync ().Copy the code
Const folderPath = './ SRC '// path fs.readdirsync (folderPath) to get the full path: Fs.readdirsync (folderPath).map(fileName => {return path.join(folderPath, fileName)}) can also filter results to return only files (excluding folders) : const isFile = fileName => { return fs.lstatSync(fileName).isFile() } fs.readdirSync(folderPath).map(fileName => { return path.join(folderPath, fileName) }) .filter(isFile)Copy the code
1.1.6 fs. Rename () or fs. RenameSync ()
Rename folders using fs.rename() or fs.renamesync (). The first argument is the current path and the second argument is the new path:Copy the code
1.1.7 fs. Rmdir () or fs. RmdirSync ()
Folders can be deleted using fs.rmdir() or fs.rmdirsync (). Deleting folders that contain content can be more complicated. The FS-Extra module is a direct replacement for the FS module and provides more functions on top of it.Copy the code
In this example, what is needed is the remove() method.
NPM install fs-extra // Install and use it like this: const fs = require('fs-extra') const folder = '/Users/joe' fs.remove(folder, Err => {console.error(err)}) can also be used with promises: Fs. Remove (folder). Then (() = > {/ / complete}). The catch (err = > {console. Error (err)})Copy the code
1.2 Path Path module
-
path.sep
The path segment separator, \ on Windows, / on Linux/macOS
-
path.delimiter
Path delimiter, on Windows; On Linux/macOS it is:
-
path.basename()
Returns the last part of the path. The second parameter filters out file extensions:
require('path').basename('/test/something') //something
require('path').basename('/test/something.txt') //something.txt
require('path').basename('/test/something.txt', '.txt') //something
Copy the code
- path.dirname()
Return to the directory portion of the path:
- path.extname()
Returns the extension portion of the path.
- path.isAbsolute()
Returns true if it is an absolute path
- path.join()
Connecting two or more parts of a path:
- path.normalize()
When containing similar..,.. Or a relative specifier, such as a double slash, tries to calculate the actual path:
- path.parse()
The path of the parsed object is the fragment that comprises it: root: the root path. Dir: indicates the folder path starting from the root path. Parse ('/users/test.txt') {root: '/', dir: '/users', base: require('path'). 'test.txt', ext: '.txt', name: 'test' }Copy the code
- path.relative()
Accept two paths as arguments. Returns the relative path from the first path to the second path, based on the current working directory.
path.resolve()