Node basics and fs module study notes

Understanding Node.js and built-in modules

What is Node.js?

Node.js® is an open-source, cross-platform JavaScript runtime environment.

node.js is an open-source, cross-platform JavaScript runtime environment .

The front-end operating environment is the browser.

Note : Browser built-in APIs such as DOM and BOM cannot be called in Node.js.

The top-level object in Node.js is global, and globalThis can also be used to access the top-level object.
The following code will report an error

// BOM
console.log(window);
console.log(history)
console.log(navigator)
console.log(location)

// DOM
console.log(document)
//AJAX
let xhr = new XMLHttpRequest();

Components of JavaScript in the browser

js core syntax: variable, data type; loop, branch, judgment; function, scope, this, etc.

WebAPI: DOM operation, BOM operation, Ajax operation based on XMLHttpRequest

Why js can be executed in the browser

Different browsers have different JavaScript parsing engines:

Chrome's V8 parsing engine has the best performance.

insert image description here

Why js can manipulate DOM and BOM

Each browser has built-in DOM and BOM API functions, so js in the browser can call them.

The JavaScript runtime in the browser

The operating environment refers to the necessary environment required for the normal operation of the code.
The V8 engine is responsible for parsing and executing JavaScript code.
The built-in API is a special interface provided by the operating environment and can only be called in the operating environment to which it belongs.

Can JavaScript be used for back-end development?

JavaScript can be developed in the background with the help of node.js, but not alone. Node.js can be used as the back-end operating environment and JavaScript to complete the back-end development.

Installation and use of node

node installation

Two versions can be downloaded from the node official website. LTS is a long-term support stable version, which is suitable for large-scale enterprise projects.

current is up to date, you can try out new features but there may be bugs and security issues.

Enter node -v in the terminal to view the installed version

The use of nodes

  1. Open the terminal win+r and enter cmd to enter the terminal
  2. Enter the path of the js file, cd can switch the path d: can switch the c drive and the d drive
  3. Run the command: node demo.js

How to open the terminal under the current path, you can enter cmd in the address bar of the current path and press Enter, or press and hold shift in the blank space without letting go of the right mouse button to open powershell.

Terminal shortcut key
↑ Quickly navigate to the last executed command
tab Quickly complete the path
esc Quickly clear the currently entered command
cls Clear the terminal

file write

The fs module can realize the interaction with the hard disk, such as the creation, deletion, renaming, and movement of files, as well as the writing and reading of file content, and related operations on folders.


If you use the fs module to manipulate files in js code, you need to import

const fs=require('fs') first

writeFile writes asynchronously

Write content to the specified file fs.writeFile(file, data[, options], callback)


file: Required parameter, a character string that needs to specify a file path, indicating the storage path of the file

data: Required parameter, indicating to write Input content

options: optional parameter, indicating what encoding format to write the file content in, the default value is utf8.

callback: Required parameter, the callback function after the file is written.

const fs = require('fs')
    fs.writeFile('./files/2.txt', 'hellonodejs', function (err) {
        
        console.log('写入成功!' + err)
        // 文件写入成功,err的值等于null
        // 文件写入失败,err的值等于一个错误对象
    })

Screenshot of running results
insert image description here

Determine whether the file was written successfully

const fs = require('fs')
    fs.writeFile('./files/2.txt', 'hellonodejs', function (err) {
        if (err) {
           return console.log('文件写入失败!'+err.message)
       }
        console.log('写入成功!' )
       
    })

run screenshot
insert image description here
insert image description here

writeFileSync write synchronously

fs.writeFileSync('./data.txt','test');

Scenarios with low performance can use synchronous writes.

appendFile/appendFileSync additional write

fs.appendFile('./files/2.txt', '这是中文附加的字', err => {
    if (err) {
        console.log('写入失败');
        return;
    }
    console.log('追加写入成功')
});

fs.appendFileSync('./files/2.txt', '\r\n这是中文附加的字')

insert image description here

createWriteStream streaming write

Syntax: fs.createWriteStream(path[,options])

Parameter description:

path file path

options option configuration (optional)


Return value: Object

Code example:

// 1.导入fs模块
const fs = require('fs');
// 2.创建写入流对象
const ws = fs.createWriteStream('./files/给翠花的信.txt');
// 3.write
ws.write('亲爱滴翠花\r\n');
ws.write('我喜欢你很久了\r\n');
ws.write('可以借五毛钱嘛\r\n');
// 4.关闭通道
ws.close();

insert image description here

Opening a file by a program consumes resources, and streaming writing can reduce the number of times the file is opened and closed.

Streaming write is suitable for scenarios where large files are written or frequently written, while writeFile is suitable for scenarios with low write frequency.

The scene written to the file

When you need to save data persistently, you should think of writing files, downloading

files,

installing software,

saving program logs, such as git

editor saving files,

video recording

file read

readFile reads asynchronously

fs.readFile(path[, options], callback)
path: Required parameter, string, indicating the path of the file.
options: optional parameter, indicating what encoding format to read the file in.
callback: A required parameter. After the file is read, the read result is obtained through the callback function.

    const fs = require('fs')
    fs.readFile('./files/demo.txt', 'utf8', function (err, dataStr) {
        console.log(err)
        console.log('------')
        console.log(dataStr)
    })

callback callback function, get the failed and successful results of reading, err, dataStr

Run node through powershell under this path. If the name of

the node 1

script file is too long, use the tab key to automatically complete it.

run screenshot
insert image description here

The value of read success err is null,

the value of read failure err is an error object, and the value of dataStr is undefined

Determine whether the file was read successfully

You can judge whether the err object is null, so as to know the result of file reading

    const fs = require('fs')
    fs.readFile('./files/demo.txt', 'utf8', function (err, result) {
        if (err) {
            return console.log('文件读取失败!' + err.message)
        }
        console.log('文件读取成功!' + result)
    })

Screenshot of running results
insert image description here

readFileSync read synchronously

const fs = require('fs');

fs.readFile('./files/给翠花的信.txt', (err, data) => {
    if (err) {
        console.log('读取失败');
        return;
    }
    console.log(data.toString());
});

Screenshot of running results
insert image description here

createReadStream streaming read

const fs = require('fs');
const rs = fs.createReadStream('./files/匆匆那年.mp3');
rs.on('data', chunk => {
    console.log(chunk.length);
    console.log(chunk.toString());
})

Streaming read chunk.length=65536 bytes corresponds to 64kb, which means streaming reads 64kb of content in storage each time. If you print chunk.toString(), there will be garbled characters, because mp3 audio cannot be printed as characters.
insert image description here

// end可选事件
rs.on('end',()=> {
    console.log('读取完成');
});

Application scenarios for reading files

  • computer boot
  • Program running
  • editor to open the file
  • view image
  • play video
  • play music
  • git view log
  • upload files
  • view chat history

file copy

The first method can use readFile to read the file content. The specific steps are to use readFileSync to read the file first, and then use writeFileSync to write the copy of the file to complete.

const fs = require(('fs'));

let data = fs.readFileSync('./files/匆匆那年.mp3');

fs.writeFileSync('./files/匆匆那年-1.mp3', data);

The second method is stream operation, create a read stream object and then create a write stream object, and bind the data event.

const fs = require(('fs'));
const rs = fs.createReadStream('./files/匆匆那年.mp3');
const ws = fs.createWriteStream('./files/匆匆那年-2.mp3');

rs.on('data', chunk => {
    ws.write(chunk);
});

Screenshot of running results
insert image description here

The second way takes up less resources, so the second way is better. Streaming reading is block reading, and the content is read one by one, occupying less memory.
You can check the memory usage by introducing the process module and calling the memoryUsage() method. rss: Indicates the total amount of memory occupied by the node process.

const fs = require(('fs'));
const process = require('process');
let data = fs.readFileSync('./files/匆匆那年.mp3');

fs.writeFileSync('./files/匆匆那年-1.mp3', data);
console.log(process.memoryUsage());//rss
const rs = fs.createReadStream('./files/匆匆那年.mp3');
const ws = fs.createWriteStream('./files/匆匆那年-2.mp3');
//绑定data事件
rs.on('data', chunk => {
    ws.write(chunk);
});
rs.on('end', () => {                     
    console.log(process.memoryUsage());//24383488
})

insert image description here

Among the rss values ​​shown in the figure, the value of the streaming operation is larger because the file is too small. When the file is relatively large, it will be obvious that the streaming operation saves more memory.
Or you can not bind the data event, and use the pipeline command to quickly realize the copy

rs.pipe(ws)

File moving and renaming

In node.js we can use rename or renameSync to move and rename files or folders

grammar

fs.rename(oldPath,newPath,callback)

fs.renameSync(oldPath,newPath)

parameter description:

oldPath file current path

newPath file new path

callback callback after operation

code example

const fs = require('fs')
fs.rename('./6文件赋值.js', './files/6文件复制.js', (err) => {
    if (err) throw err;
    console.log('移动完成')
});
fs.renameSync('./6文件赋值.js', './files/6文件复制.js');

insert image description here

file deletion

Use unlink or unlinkSync in Node.js to delete files

grammar

fs.unlink(path,callback)

fs.unlinkSync(path)

parameter description: the callback after

the path file path callback operation

code example

const fs = require('fs')

fs.unlink('./test.txt', err => {
    if (err) throw err;
    console.log('删除成功');
});
fs.unlinkSync('./test2.txt');

insert image description here
insert image description here

// 调用rm方法     rmRync
fs.rm('./论语.txt', err => {
    if (err) {
        console.log('delete successfully');
        return;
    }
    console.log('删除成功');
})

insert image description here

folder operations

With the help of Node.js capabilities, we can create, read, and delete folders

mkdir/mkdirSync create folders

readdir/readdirSync read folders

rmdir/rmdirSync delete folders

mkdir create folder

fs.mkdir(path[,options],callback)

fs.mkdirSync(path[,options])

options option configuration (optional)

The following code can create a folder named html

const fs = require('fs');
fs.mkdir('./html', err => {
    if (err) {
        console.log('创建失败');
        return;
    }
    console.log('创建成功');
});

Let's take a look at the code for recursively creating folders

fs.mkdir('./a/b/c', { recursive: true }, err => {
    if (err) {
        console.log('创建失败');
        return;
    }
    console.log('创建成功');
});

Note that recursive creation requires { recursive: true } to be successfully created.

readdir read folder

So how to read the folder

fs.readdir('./files', (err, data) => {
    if (err) {
        return console.log('读取失败');
    }
    console.log(data);
});

insert image description here

rmdir delete folder

rm;remove remove
The following code is used to demonstrate the deletion of the html folder

const fs = require('fs');
fs.rmdir('./html', err => {
    if (err) {
        return console.log('删除失败')
    }
    console.log('删除成功');
});
// 递归删除
fs.rmdir('./a', { recursive: true }, err => {
    if (err) {
        return console.log('删除失败');
    }
    console.log('删除成功');
});

insert image description here

View resource status

stat or statSync to view detailed information of resources

code example

const fs = require('fs');
fs.stat('./files/匆匆那年.mp3', (err, data) => {
    if (err) {
        return console.log('操作失败')
    }
    console.log(data);
    console.log(data.isFile());
    console.log(data.isDirectory());
});

Result value object structure: size file size, birthday creation time, mtime last modification time
isFile detects whether it is a file, isDirectory detects whether it is a folder

relative path and absolute path

const fs = require('fs');

// 相对路径
fs.writeFileSync('./index.html', 'love');
fs.writeFileSync('index.html', 'love');
fs.writeFileSync('../index.html', 'love');

// 绝对路径
fs.writeFileSync('D:/index.html', 'love');
fs.writeFileSync('/index.html', 'love');

Relative path references: references to the command line

relative path problem

When using the fs module to manipulate files. If the provided operation path is a relative path starting with ./ or .../ , it is easy to cause path dynamic splicing errors.
Reason: When the code is running, it will dynamically splice out the full path of the operated file based on the directory where the node command is executed.
insert image description here

fs.readFile('./files/1.txt','utf8',function(err.,dataStr){
    if(err){
        return console.log('读取文件失败!'+err.message)
    }
    console.log('读取文件成功!'+dataStr)
})

If there is a path splicing error problem, it is because a relative path starting with ./ or .../ is provided.

To solve this problem, you can directly provide a complete file storage path.

fs.readFile('D:\\VUE\\nodejs\\files\\1.txt','utf8',function(err,dataStr){
    if(err){
        return console.log('读取文件失败!'+err.message)
    }
    console.log('读取文件成功!'+dataStr)
})

_dirname

// __dirname表示当前文件所处目录的绝对路径
console.log(__dirname)

The value represented by __dirname will not change with the dynamic splicing of the command node path, it is a constant value
insert image description here

Best Practices

fs.readFile(__dirname+'./files/1.txt','utf8',function(err,dataStr){
    if(err){
        return console.log('读取文件失败!'+err.message)
    }
    console.log('读取文件成功!'+dataStr)
})

Practice - Exam Score Collation

Use the fs file system module to organize the test data in the score.txt file under the material directory into the score-ok.txt file

Core Implementation Steps

  1. Import the required fs file system module
  2. Use the fs.readFile() method to read the grade.txt file in the material directory
  3. Determine whether the file read failed
  4. After the file is successfully read, process the grade data
  5. Call the fs.writeFile() method to write the completed score data into the new file score-ok.txt
// 1.导入fs模块
const fs = require('fs')

// 2.调用fs.readFile()读取文件内容
fs.readFile('./素材/成绩.txt', 'utf8', function (err, dataStr) {
// 3.判断是否读取成功
    if (err) {
        return console.log('读取文件失败!'+err.message)
    }

    // console.log('读取文件成功'+dataStr)
// 4.1先把成绩的数据,按照空格进行分割
    const arrOld = dataStr.split(' ')
    
// 4.2循环分割后的数组,对每一项数据进行字符串的替换操作
    const arrNew = []
    arrOld.forEach(items => {
        arrNew.push(items.replace('=',':'))
    })
// 4.3把新数组中的每一项,进行合并,得到一个新的字符串
    const newStr = arrNew.join('\r\n')
    console.log(newStr)
       
    fs.writeFile('./files/成绩-ok.txt', newStr, function (err) {
    if (err) {
        return console.log('成绩写入失败'+err.message)
    }
    console.log('成绩写入成功')
    })
})

insert image description here
insert image description here
insert image description here

practise

batch rename

First, output all file information of the files folder

const fs = require('fs');
// 读取code文件夹
const files = fs.readdirSync('./files');
// 遍历数组
files.forEach(item => {
    console.log(item);
})

Terminal output:

PS D:\VUE\nodejs> node .\14 Batch rename.js
1.txt
2.txt
3.txt
6 File copy.js
demo.txt
Hurry that year-1.mp3
Hurry that year-2. mp3
The Year in a Hurry.mp3
Results-ok.txt
Letter to Cuihua.txt
Complete Code

const fs = require('fs');
// 读取code文件夹
const files = fs.readdirSync('./files');
// 遍历数组
files.forEach(item => {
// 拆分文件名
    let data = item.split('-');
    let [num, name] = data;
    // 判断
    if (Number(num) < 30) {
        num = '0' + num;
    }
    // 创建新的文件名
    let newName = num + '-' + name;
    // console.log(newName);
    // 重命名
    fs.renameSync(`./files/${item}`, `./files/${newName}`);
})

Summary Notes

fs provides three forms: synchronous, callback and promise-based. The synchronous API will block the thread, but for some scenarios, using the synchronous method will be more convenient and easier to understand

Try to use the promise method, the callback is easy to cause nested hell

For large files, use the file stream method to read and write files createReadStream and createWriteStream to reduce Memory usage

The three-party npm package fs-extra is an extension of fs, providing many more convenient APIs

.forEach(item => {
// 拆分文件名
    let data = item.split('-');
    let [num, name] = data;
    // 判断
    if (Number(num) < 30) {
        num = '0' + num;
    }
    // 创建新的文件名
    let newName = num + '-' + name;
    // console.log(newName);
    // 重命名
    fs.renameSync(`./files/${item}`, `./files/${newName}`);
})

Summary Notes

fs provides three forms: synchronous, callback and promise-based. The synchronous API will block the thread, but for some scenarios, using the synchronous method will be more convenient and easier to understand

Try to use the promise method, the callback is easy to cause nested hell

For large files, use the file stream method to read and write files createReadStream and createWriteStream to reduce Memory usage

The three-party npm package fs-extra is an extension of fs, providing many more convenient APIs

Guess you like

Origin blog.csdn.net/weixin_55355282/article/details/131080300