编程语言
首页 > 编程语言> > Node Second Drive

Node Second Drive

作者:互联网

原视频地址:https://www.bilibili.com/video/BV1Gr4y1h7st?p=49&spm_id_from=pageDriver
本文归属:P18-P49

擅长异步IO的node

image

事件循环

node没有一个消息队列(或者说事件队列、回调队列)
事件循环分四个阶段,每个阶段有自己的消息队列
image

深入了解: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

  1. 主版本号:当你做了不兼容的 API 修改,
  2. 次版本号:当你做了向下兼容的功能性新增,
  3. 修订号:当你做了向下兼容的问题修正。

nodemon

nodemon的出现,可以随时监听文件的变更,自动重启服务

标签:Node,console,log,require,Drive,Second,const,data,celebrity
来源: https://www.cnblogs.com/shixiu/p/16328447.html