Related articles

  • P01: Node.js tutorial from a practical perspective
  • P02: Basic node usage
  • P03: Node built-in module PATH
  • P04: Buffer for nodeAPI
  • P05: Events of node built-in module
  • P06: Node built-in module FS (1)
  • P07: Node built-in module FS (2)
  • P08: Node implements static server ~ create project
  • P09: Node implements static server ~ Hello HTTP
  • P10: Node implements static server ~ static file or folder reading

Fs file system

A simple introduction

The FS module is the built-in module of Node.js

The FS module provides an API for interacting with the file system in a manner that mimics standard POSIX functions

Note that if the operation completes successfully, the first argument will be null or undefined.

Read the file

Const fs = require('fs') // If the operation completes successfully, the first argument will be null or undefined. fs.readFile('./06_fs.js', (err, If (err) throw err console.log(data)}) // Print a binary buf /** * PS E:\ XXX \ CCC \aaa\ API > node  06_fs.js <Buffer 2f 2f 20 e6 89 80 e6 9c 89 e6 96 87 e4 bb b6 e7 b3 bb e7 bb 9f e6 93 8d e4 bd 9c e9 83 bd e5 85 b7 e6 9c 89 e5 90 8c e6 ad a5 e5 92 8c e5 bc 82 e6 ad ... > * / ` ` `Copy the code

Write files

Const fs = require('fs') fs.writefile ('./test.js', 'test test', {encoding: 'utf8' }, (err) => { if (err) throw err console.log('done! ')}) // print // add a test.js file to this file and say test test // done! ` ` `Copy the code

The output is in a specific format such as a string

  • toString()
    const fs = require('fs') // Note that if the operation completes successfully, the first argument will be null or undefined. fs.readFile('./07_fs_throw_string.js', (err, data) => {// Throw if an error occursif(err) throw err console.log(data) // Output string console.log(data.tostring ())}) <! /** * PS E:\ XXX \ XXX \ XXX \ API > node 07_fs_throw_string.js <Buffer 2f 2f 20 20 e8 be 93 E5 87 BA 20 E5 AD 97 E7 AC  a6 e4 b8 b2 0d 0a 63 6f 6e 73 74 20 66 73 20 3d 20 72 65 71 75 69 72 65 28 27 66 73 27 29 0d 0a 2f 2f ... > // const fs = require('fs') // Note that if the operation completes successfully, the first argument will be null or undefined. fs.readFile('./07_fs_throw_string.js', (err, data) => {// Throw if an error occursif(err) throw err console.log(data) // Outputs the string console.log(data.tostring ())}) */Copy the code
  • Pass parameter ~recommended
    const fs = require('fs') // Note that if the operation completes successfully, the first argument will be null or undefined. fs.readFile('./08_fs_throw_string.js'.'utf8', (err, data) => {// Throw if an error occursif(err) throw err console.log(data)}) const fs = require('fs') // // Note that if the operation completes successfully, the first argument will be null or undefined. // fs.readFile('./08_fs_throw_string.js'.'utf8', (err, data) => {// // If an error occurs pass throw //if (err) throw err
    //     console.log(data)
    // })
    Copy the code

Asynchronous synchronous

Both have to wait until the file is read (or partially read) before returning the file. What’s the difference?

  • There is no difference, or no significant difference, for individual users using the server.
  • However, in a web scenario where a large number of users are using the data, synchronization will cause the previous user processing to block the subsequent user processing.
  • Asynchrony, on the other hand, simply lets it wait alone to continue processing, achieving high concurrency.
  • All other apis have synchronized versions, as shown in Node
  • For example
    // const fs = require('fs') // Note that if the operation completes successfully, the first argument will be null or undefined. fs.readFile('./08_fs_throw_string.js'.'utf8', (err, data) => {// Throw if an error occursif(err) throw err console.log(111111111)}) // Synchronize const data = fs.readfilesync ('./09_fs_sync.js'.'utf8')
    
    console.log(data)
    
    // 打印
    /**
     * // 异步
        const fs = require('fs') // Note that if the operation completes successfully, the first argument will be null or undefined. fs.readFile('./08_fs_throw_string.js'.'utf8', (err, data) => {// Throw if an error occursif(err) throw err console.log(111111111)}) // Synchronize const data = fs.readfilesync ('./09_fs_sync.js'.'utf8')
    
        console.log(data)
        111111111
     * 
     */
    Copy the code

    You can see that synchronous methods are executed first, even if written later, because asynchronous methods are handed over to THE I/O

close