Skip to content

Commit

Permalink
Merge pull request #105 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
update file-system md
  • Loading branch information
GuoXiCheng authored Jul 26, 2024
2 parents ab87517 + 0392a96 commit 4005121
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 44 deletions.
27 changes: 25 additions & 2 deletions src/.vitepress/sidebars/nodejs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,28 @@
link: /backend/nodejs/about-nodejs
- text: EventEmitter
link: /backend/nodejs/event-emitter
- text: File System
link: /backend/nodejs/file-system
- text: 文件系统
items:
- text: 编写形式
link: /backend/nodejs/file-system/write-form
items:
- text: 同步形式
link: /backend/nodejs/file-system/write-form#同步形式
- text: 回调形式
link: /backend/nodejs/file-system/write-form#回调形式
- text: Promise 形式
link: /backend/nodejs/file-system/write-form#promise-形式
- text: 文件操作
link: /backend/nodejs/file-system/file-operation
items:
- text: 文件读取
link: /backend/nodejs/file-system/file-operation#文件读取
- text: 文件写入
link: /backend/nodejs/file-system/file-operation#文件写入
- text: 文件追加
link: /backend/nodejs/file-system/file-operation#文件追加
- text: 文件删除
link: /backend/nodejs/file-system/file-operation#文件删除
- text: 文件系统标志
link: /backend/nodejs/file-system/file-system-flag

17 changes: 17 additions & 0 deletions src/backend/nodejs/file-system/file-operation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 文件操作

## 文件读取

<<< @/../projects/javascript-sandbox/src/file-system/file-operation.ts#read

## 文件写入

<<< @/../projects/javascript-sandbox/src/file-system/file-operation.ts#write

## 文件追加

<<< @/../projects/javascript-sandbox/src/file-system/file-operation.ts#append

## 文件删除

<<< @/../projects/javascript-sandbox/src/file-system/file-operation.ts#unlink
Original file line number Diff line number Diff line change
@@ -1,42 +1,4 @@
# File System

## 编写方式

### Synchronous
同步API会阻塞事件循环,直到操作完成或失败。

通常用于启动阶段加载配置,或者在不影响主应用流程的特定场景中使用。
```js
import { readFileSync } from 'node:fs';

const result = readFileSync('data.txt');
console.log(result);
```

### Callback
回调可以避免阻塞事件循环,但是可能会导致回调地狱。
```js
import { readFile } from 'node:fs';

readFile('data.txt', (err, data) => {
if (err) console.error(err);

console.log(data);
});
```

### Promise
Promise可以避免阻塞事件循环同时避免回调地狱。可以写出近乎同步的代码风格,提升可读性。
```js
import { readFile } from 'node:fs/promises';

(async () => {
const result = await readFile('data.txt');
console.log(result);
})();
```

## 文件系统标志
# 文件系统标志

| 标志 | 描述 |
| ------- | ------------------------------------------------------------------ |
Expand All @@ -54,6 +16,3 @@ import { readFile } from 'node:fs/promises';
| `'wx'` |`'w'`相似,但如果路径存在,则失败。 |
| `'w+'` | 打开文件用于读写。文件被创建(如果它不存在)或截断(如果它存在)。 |
| `'wx+'` |`'w+'`相似,但如果路径存在,则失败。 |

## API 文档参考
- [Node.js File System Documentation](https://nodejs.org/docs/latest/api/fs.html)
48 changes: 48 additions & 0 deletions src/backend/nodejs/file-system/write-form.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 编写形式

## 同步形式

同步 API 会阻塞主线程和 NodeJS 事件循环直到操作完成。如果出现错误会抛出异常,可以使用 `try...catch` 来捕获异常。

通常用于在应用程序启动时加载配置,或者在不影响主线程的场景中使用。

```js
const { unlinkSync } = require("node:fs");

try {
unlinkSync("/tmp/hello");
console.log("successfully deleted /tmp/hello");
} catch (err) {
// handle the error
}
```

## 回调形式

回调形式不会阻塞主线程和 NodeJS 事件循环,但可能会导致回调地狱。

```js
const { unlink } = require("node:fs");

unlink("/tmp/hello", (err) => {
if (err) throw err;
console.log("successfully deleted /tmp/hello");
});
```

## Promise 形式

Promise 形式可以避免阻塞主线程和 NodeJS 事件循环,同时避免回调地狱。可以写出近乎同步的代码风格,提升可读性。

```js
const { unlink } = require("node:fs/promises");

(async function (path) {
try {
await unlink(path);
console.log(`successfully deleted ${path}`);
} catch (error) {
console.error("there was an error:", error.message);
}
})("/tmp/hello");
```

0 comments on commit 4005121

Please sign in to comment.