当前位置: 首页 > 后端技术 > Node.js

Node.js基础----学习记录(三)

时间:2023-04-03 21:25:32 Node.js

2.系统模块2.1Node运行环境提供的API是什么?因为这些API是模块化开发的,所以我们也称它们为Node运行环境提供的API是system模块2.2系统模块fs文件操作f:file文件,s:system系统,文件操作系统。constfs=require('fs');读取文件内容fs.reaFile('文件路径/文件名'[,'文件编码'],callback);2.2系统模块fs文件操作写入文件内容constcontent='

使用fs.writeFile写入文件内容

';fs.writeFile('../index.html',content,err=>{if(err!=null){console.log(err);return;}console.log('文件写入成功');});2.3系统模块路径路径操作为什么要进行路径拼接不同操作系统的路径分隔符不统一/public/uploads/avatarWindows是\/Linux是/2.4路径拼接语法path.join('path','path',...)//导入路径模块constpath=require('path');//路径拼接letfinalPath=path.join('itcast','a','b','c.css');//outputitcast\a\b\c.cssconsole.log(finialPath);2.5相对路径VS绝对路径大多数情况下使用绝对路径,因为相对路径有时是相对于命令行工具当前工作目录的.在读取文件或设置文件路径时,会选择一个绝对路径。使用__dirname获取当前文件所在的绝对路径。