Skip to content

Commit 7b7d698

Browse files
committed
chore: update docs by eslint
1 parent f90d42d commit 7b7d698

File tree

12 files changed

+148
-180
lines changed

12 files changed

+148
-180
lines changed

docs/read-books/cs-books/更了不起的Node.js.md

Lines changed: 66 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -53,37 +53,29 @@ Atwood定律:
5353
- 构建网站:用Node.js做入门开发和传统的Java、PHP开发并没有什么区别。构建成功的应用是典型的单体式应用。
5454
- 构建API:后端API接口开发用于数据库访问,将返回的数据进行包裹,以HTTP形式返回;API Proxy针对前端使用的API进行优化,使前端开发更人性化。
5555

56-
```js
56+
```json5
5757
// 常见接口返回格式:
58-
5958
{
60-
status:{
61-
code:100,
62-
message
63-
:
64-
'success'
65-
}
66-
,
67-
response:{
68-
...
69-
result
70-
...
59+
"status": {
60+
"code": 100,
61+
"message": "success"
62+
},
63+
"response": {
64+
// ...
65+
"result": []
66+
// ...
7167
}
7268
}
69+
```
7370

71+
```json5
7472
// 个人常用
7573

7674
{
77-
code:200,
78-
message
79-
:
80-
'操作成功',
81-
result
82-
:...
83-
result
84-
... // 常见false true [] {} 等结构
75+
"code": 200,
76+
"message": "操作成功",
77+
"result": [] // 常见false true [] {} 等结构
8578
}
86-
8779
```
8880

8981
- 构建RPC服务:Node.js是非常适合用于网络应用开发,其中Socket编程就是一种典型的网络应用开发场景,也就是说:"
@@ -601,18 +593,17 @@ console.log('print me first')
601593
```
602594

603595
- nextTick的作用是把laterCallback放到下一个循环事件中去执行
604-
- \_tickCallback则是一个非公开的方法,是在当前循环事件结束之后调用,以进行下一个事件循环的入口函数。
596+
- `_tickCallback`则是一个非公开的方法,是在当前循环事件结束之后调用,以进行下一个事件循环的入口函数。
605597

606-
**Node.js为事件循环维持了一个队列,nextTick负责入队列操作,\_tickCallback负责出队列操作**
598+
**`Node.js`为事件循环维持了一个队列,nextTick负责入队列操作,`_tickCallback`负责出队列操作**
607599

608600
3.uncaughtException事件
609601

610602
> 当Nodejs发现一个没有被捕获的异常时候,会触发这个事件。如果这个事件中存在回调函数,Node.js不会强制结束进程。
611603
612604
```js
613-
process.on('uncaughtException', err => {
605+
process.on('uncaughtException', (err) => {
614606
// 处理错误
615-
....
616607
})
617608
```
618609

@@ -672,7 +663,7 @@ Node.js和CommonJS的区别(主要体现在module.exports):
672663

673664
exports是一个特殊的对象,它的任何输出都将作为一个对外暴露的公共API
674665

675-
```js
666+
```textmate
676667
// 导出演示
677668
678669
const PI = Math.PI
@@ -682,7 +673,6 @@ exports.PI = PI
682673
// 引入演示
683674
684675
const PI = require('XXX')
685-
686676
```
687677

688678
特别注意的是:当module.exports和exports对象同时存在时,以module.exports为准
@@ -786,21 +776,18 @@ module.exports = {
786776
module.exports不一定非要返回实例化对象
787777

788778
```js
789-
790779
module.exports = 1
791-
module.exports = NaN
780+
module.exports = Number.NaN
792781
// 导出字符串
793782
module.exports = 'foo'
794783
// 导出对象
795-
module.exports = {foo: 'bar'}
784+
module.exports = { foo: 'bar' }
796785
// 导出数组
797786
module.exports = ['foot', 'bar']
798787
// 导出函数方法
799788
module.exports = () => {
800789

801790
}
802-
...
803-
804791
```
805792

806793
这里总结一下:
@@ -829,11 +816,9 @@ exports.name = () => {
829816
推荐最佳写法
830817

831818
```js
832-
exports = module.exports = opts => {
819+
exports = module.exports = (opts) => {
833820
// 除了工具类用exports.xxxx 其他都建议用module.exports
834-
....
835821
}
836-
837822
```
838823

839824
#### 理解模块的引用
@@ -869,11 +854,11 @@ Node.js模块分为:
869854

870855
##### 为模块包装提供的全局对象
871856

872-
- exports: CommonJS关键字
873-
- require: CommonJS关键字
874-
- module: CommonJS关键字
875-
- \_filename: 当前文件名称
876-
- \_dirname:当前文件目录
857+
- `exports`: CommonJS关键字
858+
- `require`: CommonJS关键字
859+
- `module`: CommonJS关键字
860+
- `_filename`: 当前文件名称
861+
- `_dirname`:当前文件目录
877862

878863
##### 内置process对象
879864

@@ -908,16 +893,12 @@ Nodejs中的全局对象和Javascript里的普通对象是一样的,主要是
908893
扩展变量:
909894

910895
```js
911-
912896
// 扩展debug变量,并进行加载
913-
global.debug = true;
897+
global.debug = true
914898

915899
// 使用扩展的debug变量
916900
if (debug === true) {
917-
.....
918-
919901
}
920-
921902
```
922903

923904
扩展方法:
@@ -958,7 +939,7 @@ import { readFile } from 'node:fs'
958939

959940
##### 模块导出
960941

961-
```js
942+
```textmate
962943
// 对所有内容进行导出
963944
export * from 'XXXXX'
964945
@@ -972,53 +953,46 @@ export { foot as foot_copy, bar } from 'XXXX'
972953

973954
> 导出对象的指定别名的过程叫做具名导出
974955
975-
```js
956+
```textmate
976957
export {MY_CONST as FOO, myFunc};
977958
978959
export {foot as test}
979-
980960
```
981961

982962
###### 内联具名导出
983963

984964
> 变量的声明有多种,在内联具名导出时不会受到限制
985965
986966
```js
987-
988967
// 导出var定义的foo变量
989968
export var foo;
990969
// 导出const定义的woo变量
991970
export const woo;
992971
// 导出let定义的test变量
993972
export let test;
994-
995973
```
996974

997975
函数也可以类似变量进行导出
998976

999977
```js
1000-
export function myFunc(){
1001-
// 处理逻辑
1002-
...
978+
export function myFunc() {
979+
// 处理逻辑
980+
// ...
1003981
}
1004982

1005-
export function myGenFunc(){
1006-
// 处理逻辑
1007-
...
983+
export function myGenFunc() {
984+
// 处理逻辑
985+
// ...
1008986
}
1009987

1010988
// function* 这种声明方式(function关键字后跟一个星号)会定义一个
1011989
// 生成器函数 (generator function),它返回一个 Generator 对象。
1012-
1013990
```
1014991

1015992
```js
1016-
1017993
export class MyClass {
1018994
// 类实现
1019-
...
1020995
}
1021-
1022996
```
1023997

1024998
###### 默认导出
@@ -1030,17 +1004,13 @@ export class MyClass {
10301004
```js
10311005

10321006
export default function myFunc(){
1033-
//
1034-
...
10351007
}
10361008

10371009
export default function(){
1038-
// ...
10391010
}
10401011

10411012
// 默认导出生成器函数,返回generator函数
10421013
export default function* myGenFunc(){
1043-
// ...
10441014
}
10451015

10461016
export default function* (){
@@ -1050,12 +1020,10 @@ export default function* (){
10501020
// 默认导出MyClass类
10511021
export default class MyClass{
10521022
// 类实现
1053-
// ...
10541023
}
10551024

10561025
// 默认导出匿名类
10571026
export default class{
1058-
// ...
10591027
}
10601028

10611029
// 当然,其他的也是可以的
@@ -1065,7 +1033,6 @@ export default 'Hello World';
10651033
export default 3*7;
10661034
// 匿名函数
10671035
export default (function(){});
1068-
10691036
```
10701037

10711038
### export default和export的区别
@@ -1095,7 +1062,7 @@ export default (function(){});
10951062
10961063
- 同步
10971064

1098-
> 操作必须要等待回复后才能去做其他的事情,有种至死方休的感觉。每一步都需要等待上一步完成才能进行;
1065+
> 操作必须等待回复后才能去做其他的事情,有种至死方休的感觉。每一步都需要等待上一步完成才能进行;
10991066
11001067
例如JQuery中的ajax异步请求
11011068

@@ -1125,32 +1092,32 @@ EventLoop依赖libuv库,而libuv库采用的是异步和事件驱动的风格
11251092
API:Application Programming Interface简称,异步的核心在于Nodejs SDK的API调用,然后交由EventLoop(libuv)去执行。\* \*因此Nodejs的API操作非常重要\*\*[Nodejs Api官网](http://nodejs.cn/api/)
11261093

11271094
```js
1128-
const fs=require('fs');
1095+
const fs = require('node:fs')
11291096
// 文件路劲
1130-
const path='.'
1097+
const path = '.'
11311098

11321099
// 异步读取
11331100

1134-
fs.readdir(path,funtion(err,files){
1135-
if(err){
1136-
// 出现异常
1137-
}
1138-
// 读取结果
1139-
console.log(files)
1101+
fs.readdir(path, (err, files) => {
1102+
if (err) {
1103+
// 出现异常
1104+
}
1105+
// 读取结果
1106+
console.log(files)
11401107
})
11411108
// 也可以转换为箭头函数
11421109

1143-
fs.readdir(path,(err,files)=>{
1144-
if(err){
1145-
// 出现异常
1146-
}
1147-
// 读取结果
1148-
console.log(files)
1110+
fs.readdir(path, (err, files) => {
1111+
if (err) {
1112+
// 出现异常
1113+
}
1114+
// 读取结果
1115+
console.log(files)
11491116
})
11501117

11511118
// 同步写法
11521119

1153-
const result=fs.readdirSync(path);
1120+
const result = fs.readdirSync(path)
11541121
// 输出结果
11551122
console.log(result)
11561123
```
@@ -1180,19 +1147,19 @@ callback function采用错误优先(error-first)的回调方式,而EventEmitte
11801147

11811148
```js
11821149
/**
1183-
* err :错误对象
1184-
* data : 成功时返回的数据
1185-
*/
1186-
function (err,data){
1187-
if(err){
1188-
// 存在错误
1189-
}
1190-
// 正常,则输出结果data
1191-
console.log(data)
1150+
* err :错误对象
1151+
* data : 成功时返回的数据
1152+
*/
1153+
function test(err, data) {
1154+
if (err) {
1155+
// 存在错误
1156+
}
1157+
// 正常,则输出结果data
1158+
console.log(data)
11921159
}
11931160
```
11941161

1195-
但从代码语义上来说,非空的err属于程序异常,而空的err相当于能够正常返回结果,不存在异常。
1162+
但从代码语义上说,非空的err属于程序异常,而空的err相当于能够正常返回结果,不存在异常。
11961163

11971164
##### API写法约定
11981165

@@ -1301,8 +1268,7 @@ event.on('someEvent', () => {
13011268
console.log('event 2')
13021269
})
13031270

1304-
......// 更多
1305-
1271+
// 更多 ......
13061272
```
13071273

13081274
当超过10个回调函数时会发出警告⚠,当然也可以通过`setMaxListeners`方法来改变
@@ -1316,12 +1282,11 @@ event.setMaxListeners(100)
13161282
事件传参举例:
13171283

13181284
```js
1319-
const eventEmitter = require('events')
1320-
const myEmitter = new EventEmitter();
1285+
const eventEmitter = require('node:events')
1286+
const myEmitter = new EventEmitter()
13211287

13221288
function testConnection(param) {
1323-
console.log('传递的参数',param
1324-
)
1289+
console.log('传递的参数', param)
13251290
}
13261291

13271292
myEmitter.on('test', testConnection)
@@ -1515,5 +1480,5 @@ obj.aAsync().then(obj.bAsync()).then(obj.cAsync()).catch((err) => {
15151480

15161481
在Nodejs的世界里,http是最常用的模块,它简单且效率非常高,是被应用最广泛的模块。如果说http是Node.js的核心模块,那么Stream就是核心中的核心,是保证http高效的秘密武器。相比之下,events显得极为底层,是为核心模块服务的;
15171482

1518-
**function\* 这种声明方式(function关键字后跟一个星号)会定义一个生成器函数 (generator function),它返回一个 Generator
1483+
**`function` 这种声明方式(function关键字后跟一个星号)会定义一个生成器函数 (generator function),它返回一个 Generator
15191484
对象。**

0 commit comments

Comments
 (0)