当前位置:网站首页>一文详解Nodejs中fs文件模块与path路径模块
一文详解Nodejs中fs文件模块与path路径模块
2022-07-26 10:35:00 【InfoQ】
1.初识 Node.js



- V8 引擎负责解析和执行 JavaScript 代码
- 内置 API 是由运行环境提供的特殊接口,只能在所属的运行环境中被调用
Node.js 简介
- 什么是 Node.jsNode.js 是一个基于 Chrome V8 引擎的JavaScript 运行环境官网地址 https://nodejs.org/zh-cn/
- Node.js 中的 JavaScript 运行环境
- 浏览器是 JavaScript 的前端运行环境
- Node.js 是 JavaScript 的后端运行环境
- Node.js 中无法调用 DOM 和 BOM 等浏览器内置 API
- Node.js 可以做什么
- a基于 Express 框架 http://www.expressjs.com.cn/ 可以快速构建 Web 应用
- b基于 Electron 框架 https://electronjs.org/ 可以构建跨平台的桌面应用
- c基于 restify 框架 http://restify.com/ 可以快速构建 API 接口项目
- d读写和操作数据库、创建实用的命令行工具辅助前端开发、etc…
- Node.js 怎么学
- 浏览器中的 JavaScript 学习路径
- JavaScript 基础语法 + 浏览器内置 API(DOM + BOM) + 第三方库(jQuery、art-template 等)
- Node.js 的学习路径JavaScript 基础语法 + Node.js 内置 API(fs、path、http等)+ 第三方 API 模块(express、mysql 等)
- ,可以快速定位到上一次执行的命令。
- tab键,可以快速补全文件的路径(先输入第一个字,然后使用tab键,可以快速补全路径)
- ESC,可以快速清空已输入的命令
- cls,清空powershell面板
2.fs 文件系统模块
什么是 fs 文件系统模块
const fs = require("fs")
fs.readFile(path[, option], callback)pathoptioncallback// 1. 导入 fs 模块,来操作文件
const fs = require('fs')
// 2. 调用 fs.readFile() 方法读取文件
// 参数1:读取文件的存放路径
// 参数2:读取文件时候采用的编码格式,一般默认指定 utf8
// 参数3:回调函数,拿到读取失败和成功的结果 err dataStr
fs.readFile('./files/1.txt', 'utf8', function(err, dataStr) {
// 2.1 打印失败的结果
// 如果读取成功,则 err 的值为 null
// 如果读取失败,则 err 的值为 错误对象,dataStr 的值为 undefined
console.log(err)
console.log('-------')
// 2.2 打印成功的结果
console.log(dataStr)
})
//一般逻辑可以这么写:
fs.readFile('./files/11.txt', 'utf8', function(err, dataStr) {
if (err) {
return console.log('读取文件失败!' + err.message)
}
console.log('读取文件成功!' + dataStr)
})
fs.writeFile(path, data[, option], callback)pathdataoptioncallbackconst fs = require('fs')
// 2. 调用 fs.writeFile() 方法,写入文件的内容
// 参数1:表示文件的存放路径
// 参数2:表示要写入的内容
// 参数3:回调函数
fs.writeFile('./files/3.txt', 'ok123', function(err) {
// 2.1 如果文件写入成功,则 err 的值等于 null
// 2.2 如果文件写入失败,则 err 的值等于一个 错误对象
// console.log(err)
//判断是否写入成功
if (err) {
return console.log('文件写入失败!' + err.message)
}
console.log('文件写入成功!')
})
console.log(__dirname)
fs.readFile(__dirname + '/files/1.txt', 'utf8', function(err, dataStr) {
if (err) {
return console.log('读取文件失败!' + err.message)
}
console.log('读取文件成功!' + dataStr)
})
3.path 路径模块
const path = require("path")
path.join(...paths)- ...paths <string> 路径片段的序列
- 返回<string>注意:凡是涉及到路径拼接的操作,都要使用 path.join() 方法进行处理。不直接使用+进行字符串的拼接
const path = require('path')
const fs = require('fs')
// 注意: ../ 会抵消前面的路径
const pathStr = path.join('/a', '/b/c', '../../', './d', 'e')
console.log(pathStr) // \a\b\d\e
// 取代fs.readFile(__dirname + '/files/1.txt', ...)
fs.readFile(path.join(__dirname, './files/1.txt'), 'utf8', function(err, dataStr) {
if (err) {
return console.log(err.message)
}
console.log(dataStr)
})
path.basename(path[, ext])- path <string>必选参数,表示一个路径的字符串
- ext <string>可选参数,表示文件扩展名
- 返回 <string> 路径的最后一部分
const path = require('path')
// 定义文件的存放路径
const fpath = '/a/b/c/index.html'
const fullName = path.basename(fpath)
console.log(fullName) // index.html
const nameWithoutExt = path.basename(fpath, '.html')
console.log(nameWithoutExt) // index
path.extname(path)- path <string> 必选参数,表示一个路径的字符串
- 返回 <string> 返回得到的扩展名字符串
const path = require('path')
// 这是文件的存放路径
const fpath = '/a/b/c/index.html'
const fext = path.extname(fpath)
console.log(fext)//输出.html
边栏推荐
- 比较器(Comparable与Comparator接口)
- Comparison of packet capturing tools fiddler and Wireshark
- 关于硕博士开题报告编写的思考
- Parallelism, concurrency and several directions for high concurrency optimization
- Inheritance method of simplified constructor (II) - class inheritance in ES6
- 移动端H5开发常用技巧总结
- 少了个分号
- Okaleido生态核心权益OKA,尽在聚变Mining模式
- Redis特殊数据类型使用场景
- 控制随机抽中几率 [ C# | Random ]
猜你喜欢
![Structure of [Halcon vision] operator](/img/d9/e16ea52cea7897e3a1d61d83de472f.png)
Structure of [Halcon vision] operator

Introduction to data analysis | kaggle Titanic mission

js翻页、kkpager.js翻页

Tradingview tutorial

Okaleido ecological core equity Oka, all in fusion mining mode

canvas上传图片base64-有裁剪功能-Jcrop.js

第4期:大学生提前职业技能准备之一

【论文下饭】Deep Mining External Imperfect Data for ChestX-ray Disease Screening
![[Halcon vision] morphological expansion](/img/ce/abaca036fce5b67dfe6ac361aecfea.png)
[Halcon vision] morphological expansion
![[leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间](/img/e7/a48bb5b8a86cbc4cd5b37bb16661a8.png)
[leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间
随机推荐
Our Web3 entrepreneurship project is yellow
超图 影像 如何去除黑边(两种方法)
Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
第8期:云原生—— 大学生职场小白该如何学
从蚂蚁的觅食过程看团队研发(转载)
Introduction to Phoenix (Level 1: Phoenix installation, level 2: Phoenix basic grammar)
第5期:大学生入职必备技能之二
About the declaration and definition of template functions [easy to understand]
[Halcon vision] image gray change
Oracle创建索引
Redis Docker实例与数据结构
C language calculation date interval days
反射机制简述
404页面和路由钩子
[Halcon vision] software programming ideas
[Halcon vision] Fourier transform of image
【dectectron2】跟着官方demo一起做
Dry goods likeshop takeout order system is open source, 100% open source, no encryption
algorithm
卸载魅族应用商店