// 1. 通过模块的名字fs对模块进行引用 const fs = require(‘fs‘); // 2. 通过模块内部的readFile读取文件内容 fs.readFile(‘../css/base.css‘, ‘utf-8‘, (err, doc) => { // 如果文件读取发生错误,参数err的值为错误对象,否则err的值为null // doc参数为文件内容 if(err === null) { // 在控制台输出文件内容 console.log(doc); } });
// 1. 通过模块的名字fs对模块进行引用 const fs = require(‘fs‘); const content = ‘<h3>正在使用fs.writeFile写入文件内容</h3>‘; fs.writeFile(‘./index.html‘, content, err => { if(err != null) { console.log(err); return; } console.log(‘文件写入成功‘); });
为什么要进行路径拼接
const path = require(‘path‘); const finalpath = path.join(‘public‘, ‘uploads‘, ‘avatar‘); console.log(finalpath);
原文:https://www.cnblogs.com/guwufeiyang/p/13216699.html