Node.js study notes - the next day

1. What is the system module

Node API provided by the operating environment, because these API are developed in a modular way, all of us also known as Node API provided by the operating environment for the system modules

3.1 operating system module file fs

   //f :file 文件 , s :system 系统,文件操作系统。
   const fs = require('fs');

    //读取文件内容
    fs.readFile('文件路径/文件名称‘[,'文件编码'],callback);

2. The file system module operation fs

Read the file syntax examples

    //读取上一级css目录下中的base.css
    fs.readFile('../css/base.css','utf-8', (err,doc)=>{
    //如果文件读取发生错误 参数err 的值为错误对象,否则err的值为null
    //doc参数为文件内容
    if(err==null){
        console.log(doc);
     }
   }

Write to the file contents

    fs.writeFile('文件路径/文件名称','数据',callback);
    const content = '<h3>正在使用fs.writeFile写入文件内容</h3>'
    fs.writeFile('../index.html',content,err=>{
        if(err !=null){
            console.log(err);
            return;
        }
        console.log('文件写入成功');
    }

Guess you like

Origin www.cnblogs.com/foreverLuckyStar/p/12020220.html