Skip to content

Commit c77cd8a

Browse files
张东张东
张东
authored and
张东
committed
feat:添加node
1 parent 4bb8fc3 commit c77cd8a

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed

.vitepress/config.mts

+9
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export default defineConfig({
5454
],
5555
},
5656
{ text: "性能优化", link: "/optimization/image" },
57+
{ text: "Node", link: "/node/initial" },
5758
],
5859

5960
sidebar: {
@@ -181,6 +182,14 @@ export default defineConfig({
181182
],
182183
},
183184
],
185+
"/node/": [
186+
{
187+
text: "Node",
188+
items: [
189+
{ text: "概述", link: "/node/initial" },
190+
],
191+
},
192+
],
184193
"/browser/about/": [
185194
{
186195
text: "about",

node/initial.md

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
---
2+
layout: doc
3+
outline: deep
4+
---
5+
## 与浏览器类比
6+
都是js运行时环境,node基于V8引擎(c++),但是node不具有webapi,例如dom,bom.但是提供了一些特有的api
7+
8+
node支持CommonJs,ES模块系统(since Node.js v12),浏览器正在进行ES模块系统的实施
9+
10+
## 获取文件信息
11+
1. fs.stat
12+
```js
13+
const fs = require('node:fs');
14+
fs.stat('/Users/joe/test.txt', (err, stats) => {
15+
if (err) {
16+
console.error(err);
17+
}
18+
// we have access to the file stats in `stats`
19+
});
20+
```
21+
2. fs.statSync
22+
23+
阻塞线程直到文件统计信息准备就绪
24+
```js
25+
const fs = require('node:fs');
26+
try {
27+
const stats = fs.statSync('/Users/joe/test.txt');
28+
} catch (err) {
29+
console.error(err);
30+
}
31+
```
32+
3. fs.stat
33+
34+
引入库不同node:fs/promises,基于promise
35+
```js
36+
const fs = require('node:fs/promises');
37+
async function example() {
38+
try {
39+
const stats = await fs.stat('/Users/joe/test.txt');
40+
stats.isFile(); // true
41+
stats.isDirectory(); // false
42+
stats.isSymbolicLink(); // false
43+
stats.size; // 1024000 //= 1MB
44+
} catch (err) {
45+
console.log(err);
46+
}
47+
}
48+
example();
49+
```
50+
## 文件路径
51+
### 从路径获取信息
52+
给定一条路径,可以使用以下方法从中提取信息:
53+
54+
* dirname:获取文件的父文件夹
55+
* basename:获取文件名部分
56+
* extname:获取文件扩展名
57+
```js
58+
const path = require('node:path');
59+
const notes = '/users/joe/notes.txt';
60+
path.dirname(notes); // /users/joe
61+
path.basename(notes); // notes.txt
62+
path.extname(notes); // .txt
63+
```
64+
可以通过指定第二个参数来获取不带扩展名的文件名basename
65+
```js
66+
path.basename(notes, path.extname(notes)); // notes
67+
```
68+
### 使用路径
69+
1. path.join
70+
71+
拼接路径
72+
```js
73+
const name = 'joe';
74+
path.join('/', 'users', name, 'notes.txt'); // '/users/joe/notes.txt'
75+
```
76+
2. path.resolve
77+
78+
计算相对路径的绝对路径
79+
[参考](https://juejin.cn/post/7319418651070119999#heading-1)
80+
81+
3. path.normalize
82+
当它包含相对说明符(如或..或双斜杠)时,它将尝试计算实际路径
83+
```js
84+
path.normalize('/users/joe/..//test.txt'); // '/users/test.txt'
85+
```
86+
## 读取文件
87+
88+
向其传递文件路径、编码和将使用文件数据(和错误)调用的回调函数
89+
1. fs.readFile
90+
```js
91+
const fs = require('node:fs');
92+
fs.readFile('/Users/joe/test.txt', 'utf8', (err, data) => {
93+
if (err) {
94+
console.error(err);
95+
return;
96+
}
97+
console.log(data);
98+
});
99+
```
100+
2. fs.readFileSync
101+
同步版本
102+
3. promise形式
103+
```js
104+
const fs = require('node:fs/promises');
105+
async function example() {
106+
try {
107+
const data = await fs.readFile('/Users/joe/test.txt', { encoding: 'utf8' });
108+
console.log(data);
109+
} catch (err) {
110+
console.log(err);
111+
}
112+
}
113+
example();
114+
```
115+
116+
这三个fs.readFile(),fs.readFileSync()并fsPromises.readFile()在返回数据之前读取内存中文件的全部内容。
117+
118+
这意味着大文件将对您的内存消耗和程序执行速度产生重大影响。
119+
120+
在这种情况下,更好的选择是使用流读取文件内容。
121+
122+
## 写入
123+
与读取类似,api是`writeFile`
124+
125+
如果不想全部覆盖可以使用`appendFile`
126+
127+
## 处理文件夹
128+
### 检查文件夹是否存在
129+
fs.access
130+
### 创建文件夹
131+
fs.mkdir
132+
### 读取文件夹信息
133+
fs.readdir
134+
135+
读取文件夹的内容,包括文件和子文件夹,并返回它们的相对路径
136+
137+
读取完整路径
138+
```js
139+
fs.readdirSync(folderPath).map(fileName => {
140+
return path.join(folderPath, fileName);
141+
});
142+
```
143+
### 重命名文件夹
144+
fs.rename
145+
146+
## 终端
147+
## 从命令行执行node脚本
148+
最常见的是node 文件名执行,这样明确告诉终端使用node运行该文件
149+
150+
也可以这样
151+
```js
152+
#!/usr/bin/env node
153+
```
154+
## 读取环境变量
155+
process.env
156+
## 输出内容到终端
157+
chalk progress
158+
## 接受来自命令行的输入
159+
inquirer

0 commit comments

Comments
 (0)