# 文章目录
# 一、Path 路径模块常用方法
官网直达:Path 路径模块 (opens new window)
path 模块用于对路径和文件进行处理,提供了很多好用的方法。
提示:以下两个是全局变量,和 path 没关系。
__dirname是当前目录名称
__filename是当前绝对地址
1
2
2
// 当前目录名称
console.log("__dirname: ", __dirname);
//__dirname: D:\code\Project\study\Node\4-内置模块\1-path
演练.js;
// 文件绝对地址
console.log("__filename: ", __filename);
// __filename: D:\code\Project\study\Node\4-内置模块\1-path\1-路径的
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
需求引出:
我们知道在 Mac OS、 Linux 和 window 上的路径是不一样的
- window 上会使用
\
或者\\
来作为文件路径的分隔符,当然目前也支持/
- 在 Mac OS、 Linux 的 Uniⅸ 操作系统上使用
/
来作为文件路径的分隔符
那么如果我们在 window 上使用\
来作为分隔符开发了一个应用程序,要部署到 Linux 上面应该怎么办呢?
- 显然路径会出现一些问题
- 所以为了屏蔽他们之间的差异,在开发中对于路径的操作我们可以使用 path 模块
底层实现方式:可移植操作系统接口(英语: Portable Operating System Interface,缩写为 POSⅨ)
- Linux 和 Mac OS 都实现了 POSX 接口
- Window 部分电脑实现了 POSX 接口
# 1.1 path.resolve 拼接路径
假如现在我有一个文件名称,我想让他和某个路径拼接成一个文件路径,那应该怎么做呢?
答案是使用path.resolve
const path = require("path");
const basepath1 = "\\user\\are";
const basepath2 = "/user/are";
const filename = "abc.txt";
const filePath1 = path.resolve(basepath1, filename);
const filePath2 = path.resolve(basepath2, filename);
console.log("filePath1: ", filePath1);
console.log("filePath2: ", filePath2);
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
output:
filePath1: D:\user\are\abc.txt
filePath2: D:\user\are\abc.txt
1
2
2
使用 path 模块的 resolve 即可拼接完成。
# 1.2 path.extname(扩展名)
该方法可返回一个 字符串中的扩展名
ext => extension
扩展、伸缩的
path.extname("index.html");
// 返回: '.html'
path.extname("index.coffee.md");
// 返回: '.md'
path.extname("index.");
// 返回: '.'
path.extname("index");
// 返回: ''
path.extname(".index");
// 返回: ''
path.extname(".index.md");
// 返回: '.md'
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 1.3 dirname(目录名)、basename(文件名)
let dir = "D:/code/Project/study/Node/test.js";
console.log("目录:", path.dirname(dir));
console.log("文件名称:", path.basename(dir));
console.log("后缀名是:", path.extname(dir));
// 目录: D:/code/Project/study/Node
// 文件名称: test.js
// 后缀名是: .js
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 1.4 path.join path.resolve 路径拼接
path.join 和 path.resolve 都是拼接路径的
const basepath = "/user/are";
const filename = "test.js";
console.log(path.join(basepath, filename));
// \user\are\test.js
console.log(path.resolve(basepath, filename));
// \user\are\test.js
1
2
3
4
5
6
2
3
4
5
6
既然效果都一样那有什么区别呢?
resolve 会判断拼接的字符串中,开头是否有以/或./或../开头的路径
如果有:则会把当前的文件路径和传入的路径进行拼接
// 把 basepath开头的 / 去掉之后
const basepath = "user/are";
const filename = "test.js";
console.log(path.join(basepath, filename));
// resolve 会判断拼接的字符串中,开头是否有以/或./或../开头的路径
console.log(path.resolve(basepath, filename));
// user\are\test.js
// D:\code\Project\study\Node\4-内置模块\1-path\user\are\test.js
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
具体的:
const basepath2 = "user/are";
const basepath1 = "/user/are";
const basepath3 = "./user/are";
const basepath4 = "../user/are";
const filename = "test.js";
console.log(path.resolve(basepath1, filename));
console.log(path.resolve(basepath2, filename));
console.log(path.resolve(basepath3, filename));
console.log(path.resolve(basepath4, filename));
// D:\user\are\test.js
// D:\code\Project\study\Node\4-内置模块\1-path\user\are\test.js
// D:\code\Project\study\Node\4-内置模块\1-path\user\are\test.js
// D:\code\Project\study\Node\4-内置模块\user\are\test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 二、fs 文件系统
官网直达:fs 文件系统 (opens new window)
fs 是 File System 的缩写,表示文件系统。
fs 模块有非常多的 API,但是这些 API 大多数都提供三种操作方式:
- 方式一:同步操作文件:代码会被阻塞,不会继续执行
- 方式ニ:异步回调函数操作文件:代码不会被阻塞,需要传入回调函数,当获取到结果时,回调函数被执行
- 方式三:异步 Promise 操作文件:代码不会被阻塞,通过 fs promises 调用方法操作,会返回一个 Promise 可以通过 then、 catch 进行处理
示例:
const fs = require("fs");
const filePath = "./abc.txt";
// 读取文件信息(不是内容) 大小,创建时间等
// 1.方式一 同步操作
// const fileInfo1 = fs.statSync(filePath)
// console.log('fileInfo1: ', fileInfo1);
// 2.方式二 异步操作
// fs.stat(filePath,(err,info) =>{
// if(err){
// console.log(err);
// return
// }
// console.log(info);
// })
// 3.方式三 Promise
fs.promises.stat(filePath).then(
(res) => {
console.log(res);
},
(err) => {
console.log(err);
}
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 2.1 递归调用,获取目录中的所有文件
当前目录形式:
const fs = require("fs");
const path = require("path");
const dirname = "./are";
//递归调用,获取目录中的所有文件
function getFiles(dirname) {
// 开启 返回目录的类型
fs.readdir(dirname, { withFileTypes: "true" }, (err, files) => {
// console.log(files);
files.forEach((item) => {
// 判断是否是目录
if (item.isDirectory()) {
const filePath = path.resolve(dirname, item.name);
getFiles(filePath);
} else {
console.log(item.name);
}
});
});
}
getFiles(dirname);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
输出:
text.txt
text2.txt
text3.txt
1
2
3
2
3
# 三、events 模块
Node 中的核心 API 都是基于异步事件驱动的
- 在这个体系中,某些对象(发射器( Emitters))发出某一个事件
- 我们可以监听这个事件(监听器 Listeners),并且传入的回调函数,这个回调函数会在监听到事件时调用
发出事件和监听事件都是通过 EventEmitter 类来完成的,它们都属
于 events 对象。
- emitter.on( eventname, listener):监听事件,也可以使用
addlistener - emitter.off(eventname, listener):移除事件监听,也可以使
用 remove.Listener; - emitter.emit( eventnamel,args)):发出事件,可以携带一
些参数;