1. What is a Node. Js

    Is JavaScript server-side running environment, can let programmers use JavaScript to achieve server-side programming;Copy the code
  2. The component of JavaScript in Node.js

    ECMAScript core: var for if.... Global members: setTimeout, console... Core API modules are the apis provided by the Node platform. These apis are unique to the Node platform.Copy the code
  3. Fs.readfile ()

    Fs.readfile takes three arguments:

    Parameter 1: indicates the path of the file to be read

    Parameter 2: what encoding format is used to read the specified file. The default encoding format is NULL

    Parameter 3: indicates that when the file has been read, the callback function is called to process the read result:

    The callback takes two arguments: the first argument is an Error object, and the second argument is the result of a successful read

const fs = require('fs')
fs.readFile('./files/11.txt'.'utf-8'.function (err, data) {
  if (err) return console.log('File reading failed:' + err.message)
  console.log('File read successfully, content is' + data)
})
Copy the code

4. Write fs.writefile ()

Parameter 1: path A string to which file to write the contents of the file. Parameter 2: The data to be written can be given a string. Parameter 3: Optional parameter to which format to write the contents of the file. In the callback function, there is only one parameter, the err error objectCopy the code
const fs = require('fs')
fs.writeFile('./files/2.txt'.'222'.(err) = > {
  if (err) return console.log('Write file failed! ' + err.message)
  console.log('File write succeeded! ')})Copy the code

Note: If the file to be written does not exist when using fs.writeFile, write directly; If the file path to be written already exists, the previous file is overwritten

5. Append fs.appendfile ()

Parameter 1: indicates the file to be appended, specifying the path of a file. Parameter 2: indicates the specific content to be appended, which can be passed as a string. Parameter 3: Optional. If this parameter is omitted, it is appended in utF8 format by default. Parameter 4: indicates the error result of the callback parameter err after the appending is completeCopy the code
// 1. Import fs file operation module
const fs = require('fs')

Call fs.appendFile to append the file content
fs.appendFile('./files/3.txt'.'\n333'.(err) = > {
  if (err) return console.log('Failed to append file! ' + err.message)
  console.log('File appended successfully! ')})Copy the code
  1. CopyFile fs.copyfile ()

    fs.copyFile(src, dest[, mode], callback)

    Parameter 1: The source file name to copy.

    Parameter 2: the target file name of the copy operation.

    Parameter 3: Optional parameter used for the modifier of the copy operation. Default value: 0

    Parameter 4: callback function

    The callback function has only one argument, the error object

const fs = require('fs')

fs.copyFile(__dirname + '/files/1.txt', __dirname + '/files/1-copy.txt'.(err) = > {
  if (err) return console.log('Copy failed:' + err.message)
  console.log('Copy successful! ')})Copy the code

Note: If the target file already exists, it will be overwritten

  1. Path problem in FS

When a method in FS is called, if the operation path provided is a relative path, the relative path of the provided file will be spliced according to the disk directory when the node command is executed, which may cause problems.

In Node, __dirname represents the disk directory in which the current file resides.

In the future use of FS module, the path involved, use absolute path

fs.readFile(__dirname + '/files/1.txt'.'utf8'.(err, data) = > {
  if (err) return console.log(err.message)
  console.log(data)
})

Copy the code

Add: the difference between __dirname and __filename

// __dirname indicates the root directory where the current file is executed.
console.log(__dirname) // C:\Users\xxx\Desktop\day1
// __filename indicates the full path of the current file, which contains the name of the file
console.log(__filename) // C:\Users\xxx\Desktop\day1\test.js

Copy the code
  1. Path Path module

The path module handles file and directory paths

7.1 path.join() Indicates the joining path

path.join([…paths])

Parameter: A sequence that receives a path fragment, which must be a string

Return value: a string

Import the path module
const path = require('path')
const fs = require('fs')

path.join(Directory '1', {}, Directory '2');
TypeError: Path must be a string. Received {}' TypeError: Path must be a string.

path.join(Directory '/ 1'.Directory '2'.'Directory 3/ Directory 4'.Directory '5'.'.. ');
// Return: '/ directory 1/ directory 2/ directory 3/ directory 4'Note: splicing is in accordance with the general operation,.. It means scroll up one level, so the top item5That cancels out.//path.join() When joining paths, the path name./ path and /path have the same effect
const result = path.join('c:/'.'a'.'./b'.'/c'.'./d/e'.'f'.'.. /g')                  
console.log(result) // c:\a\b\c\d\e\g


fs.readFile(path.join(__dirname, './files/1.txt'), 'utf8'.(err, dataStr) = > {
  if (err) return console.log(err.message)
  console.log(dataStr)
})

Copy the code

Path other method attributes, if useful, please refer to the official documentation, not recorded here.

— — — — — — — — — — — — — end — — — — — — — — — — — — — — — — —

Wish to see the students can have harvest (#^.^#)!

Welcome to point out any mistakes, thank you!