原视频地址:https://www.bilibili.com/video/BV1Gr4y1h7st?p=49&spm_id_from=pageDriver
本文归属:P18-P49
擅长异步IO的node
- node保存了一些线程作为线程池,遇到文件读取等等异步操作时交给自己的某个线程单独处理;
- 而对于网络请求,则直接与操作系统交互,毕竟操作系统更擅长网络通信。
- 事件循环处理所有的回调事件
事件循环
node没有一个消息队列(或者说事件队列、回调队列)
事件循环分四个阶段,每个阶段有自己的消息队列
- 定时器阶段
- 轮询阶段
- 检测阶段
- 关闭阶段
深入了解:https://nodejs.org/zh-cn/docs/guides/event-loop-timers-and-nexttick/
node的基础设计模式--观察者设计模式
// 1
const EventEmitter = require('events');
const celebrity = new EventEmitter();
// Subcribe to celebrity for Observer1
celebrity.on('race win', (result) => {
if (result === 'win') {
console.log('Congratulations!You are the best!')
}
});
// Subcribe to celebrity for Observer2
celebrity.on('race win', () => {
console.log('Boo i can be the best!')
});
process.on('exit',(code) => {
console.log('Process exit event with',code
})
celebrity.emit('race win');
// 2
const EventEmitter = require('events');
const celebrity = new EventEmitter();
// Observer 1
celebrity.on('race',(result) => {
if (result === '‘'win') {
console.log('You are the best!');
}
});
// Observer 2
celebrity.on('race',(result) => {
if (result === 'lost') {
console.log('I can be better!');
}
});
process.on('exit',(code) => {
console.log('Process exit event with:',code) // code 为0代表正常退出
})
celebrity.emit('race','win');
celebrity.emit('race','lost');
module
// P33 event.js
// 1
const http = require('http');
const req = http.request('http://www.baidu.com',(res) => {
res.on('data',(chunk) => {
console.log(`Data chunk:${chunk}`);
});
res.on('end',() => {
console.log('No more data');
});
});
req.end();
// 2
// const {request} = require('https');
const {get} = require('https');
const req = get('https://www.baidu.com',(res) => {
res.on('data',(chunk) => {
console.log(`Data chunk:${chunk}`);
});
res.on('end',() => {
console.log('No more data');
});
});
// req.end();
// P34,35 https.mjs
const { send } = require('./request');
const { read } = require('./response');
const makeRequest = (url, data) => {
send(url, data);
return read();
}
const resData = makeRequest('https://baidu.com', 'hello');
console.log(resData)
// P37 ES6写法:文件需要改成.mjs
// import { send } from './request.mjs'
// import { read } from './response.mjs'
// const makeRequest = (url, data) => {
// send(url, data);
// return read();
// }
// const resData = makeRequest('https://baidu.com', 'hello');
// console.log(resData)
console.log(require.cache)
// request.mjs
const REQUEST_TIMEOUT = 500;
const encrypt = (data) => {
return 'encrypted data';
}
const send = (url,data) => {
const encryptedData = encrypt(data);
console.log(encryptedData)
}
// module.exports = {
// REQUEST_TIMEOUT,
// send
// }
export {
REQUEST_TIMEOUT,
send
}
// response.mjs
const decrypt = (data) => {
return 'decryted data'
}
const read = () => {
return decrypt('data')
}
// module.exports = {
// read
// }
export {
read
}
·.mjs·文件格式用于ES6写法
node模块
- 会缓存加载的模块,不会重复加载
const { send } = require('./request');
const { read } = require('./response');
const { REQUEST_TIMEOUT } = require('./request');
// 第二次加载来自'./request'的函数时,不会重新加载,而是在缓存中寻找
console.log(require.cache)
module.exports = {
...require('./request'),
...require('./response')
}
module vs package
npm init -y
// 初始化npm并全部选y。会得到package.json
Node_modules会保存npm包的完整源代码
Semantic Versioning 2.0.0
nodemon
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。