最近工作一直很忙,没时间更新,谅解,这次准备更新一次node教程,本课堂将持续更新,每周坚持更新一到两章,希望对大家有一些小帮助吧:
一、首先什么是node?
1/Node.js 是一个基于 Chrome V8 引擎的 javascript 运行环境。
2/Node.js 使用了一个事件驱动、非阻塞式 I/O 的模型,使其轻量又高效。
3/Node.js 的包管理器 npm,是全球最大的开源库生态系统。
node安装不介绍了,官网直接安装即可,傻瓜式的下一步 ,在这就不耽误时间了:
二、node初体验:
1 //引入http模块
2 const http = require(‘http‘);
3 // 新建一个函数 作为sever的回调函数 其中req 是请求相关的内容 res是返回相关的内容
4 const func = (req,res) => {
5 //加上这句话防止控制台输出两边;
6 if (req.url !== ‘/favicon.ico‘){
7 console.log(req.url);
8 res.write(‘<h1>11111</h1>‘);
9 //res.end是结束返回内容。
10 res.end();
11 }
12
13 }
14 // 建立sever,并且监听7001端口
15 http.createServer(func).listen(7001);
16 //在终端中使用node fillname.js 即可开启。在浏览器中使用http://localhost:7001/ 即可访问
三、监听器event的简单使用方法:
1 //倒入events模块
2 const event = require(‘events‘);
3 //event的唯一模块,核心功能就是:触发事件和监听事件
4 evt = new event.EventEmitter();
5
6 function eventHandler() {
7 console.log(‘监听‘);
8 }
9 //on开始监听一个指定事件,
10 evt.on(‘event‘,eventHandler);
11 //emit 触发制定事件
12 evt.emit(‘event‘);
四、event的方法:
1、once。只执行一次 不会触发多边(以及模拟异步执行);
1 const event = require(‘events‘);
2 evt = new event.EventEmitter();
3 function eventHandler() {
4 console.log(‘1111‘);
5 }
6 evt.once(‘event‘, eventHandler);
7
8 setTimeout(() => {
9 console.log(1);
10 evt.emit(‘event‘);
11 }, 2000);
12
13 setTimeout(() => {
14 console.log(0);
15 evt.emit(‘event‘);
16 }, 1000);
2、removeListener/removeAllListeners. 移除指定的监听器
1 const event = require(‘events‘);
2 const eventEmitter = new event.EventEmitter();
3 function func() {
4 console.log(‘func执行‘);
5 }
6 function func2() {
7 console.log(‘function2 执行‘);
8 }
9 eventEmitter.on(‘eventEmitterName‘, func);
10 eventEmitter.on(‘eventEmitterName‘, func2);
11
12 eventEmitter.removeListener(‘eventEmitterName‘, func);
13 eventEmitter.emit(‘eventEmitterName‘);
3、setMaxListeners设置监听事件的最大值(默认最大值10)+listeners。返回指定的事件的数组
1 const event = require(‘events‘); 2 const eventEmmiter = new event.EventEmitter(); 3 const sever = require(‘http‘).createServer(); 4 sever.setMaxListeners(15); 5 sever.on(‘request‘,(req,res) =>{ 6 if(req.url !== ‘/favicon.ico‘){ 7 res.writeHeader(200, { 8 "Content-type": "text/html" 9 }); 10 console.log(‘request begin‘); 11 res.end(‘haha‘); 12 } 13 14 }).listen(3000); 15 sever.on(‘request‘, (req, res) => { 16 if (req.url !== ‘/favicon.ico‘) { 17 res.writeHeader(200, { 18 "Content-type": "text/html" 19 }); 20 console.log(‘request begin2‘); 21 res.end(‘haha1‘); 22 } 23 24 }) 25 sever.on(‘request‘, (req, res) => { 26 if (req.url !== ‘/favicon.ico‘) { 27 res.writeHeader(200, { 28 "Content-type": "text/html" 29 }); 30 console.log(‘request begin3‘); 31 res.end(‘haha4000‘); 32 } 33 34 }).listen(4000); 35 sever.on(‘request‘, (req, res) => { 36 if (req.url !== ‘/favicon.ico‘) { 37 res.writeHeader(200, { 38 "Content-type": "text/html" 39 }); 40 console.log(‘request begin3‘); 41 res.end(‘haha4000‘); 42 } 43 44 }).listen(4000); 45 sever.on(‘request‘, (req, res) => { 46 if (req.url !== ‘/favicon.ico‘) { 47 res.writeHeader(200, { 48 "Content-type": "text/html" 49 }); 50 console.log(‘request begin4‘); 51 res.end(‘haha1‘); 52 } 53 54 }).listen(3000); 55 sever.on(‘request‘, (req, res) => { 56 if (req.url !== ‘/favicon.ico‘) { 57 res.writeHeader(200, { 58 "Content-type": "text/html" 59 }); 60 console.log(‘request begin5‘); 61 res.end(‘haha1‘); 62 } 63 64 }).listen(3000); 65 sever.on(‘request‘, (req, res) => { 66 if (req.url !== ‘/favicon.ico‘) { 67 res.writeHeader(200, { 68 "Content-type": "text/html" 69 }); 70 console.log(‘request begin6‘); 71 res.end(‘haha1‘); 72 } 73 74 }).listen(3000); 75 sever.on(‘request‘, (req, res) => { 76 if (req.url !== ‘/favicon.ico‘) { 77 res.writeHeader(200, { 78 "Content-type": "text/html" 79 }); 80 console.log(‘request begin2‘); 81 res.end(‘haha1‘); 82 } 83 84 }).listen(3000); 85 sever.on(‘request‘, (req, res) => { 86 if (req.url !== ‘/favicon.ico‘) { 87 res.writeHeader(200, { 88 "Content-type": "text/html" 89 }); 90 console.log(‘request begin2‘); 91 res.end(‘haha1‘); 92 } 93 94 }).listen(3000); 95 sever.on(‘request‘, (req, res) => { 96 if (req.url !== ‘/favicon.ico‘) { 97 res.writeHeader(200, { 98 "Content-type": "text/html" 99 }); 100 console.log(‘request begin2‘); 101 res.end(‘haha1‘); 102 } 103 104 }).listen(3000); 105 sever.on(‘request1‘, (req, res) => { 106 if (req.url !== ‘/favicon.ico‘) { 107 res.writeHeader(200, { 108 "Content-type": "text/html" 109 }); 110 console.log(‘request begin2‘); 111 res.end(‘haha9‘); 112 } 113 114 }).listen(3000); 115 /* 116 返回监听指定事件的数组 117 */ 118 const arr = sever.listeners(‘request1‘) 119 console.log(arr.toString()); 120 /*(req, res) => { 121 if (req.url !== ‘/favicon.ico‘) { 122 res.writeHeader(200, { 123 "Content-type": "text/html" 124 }); 125 console.log(‘request begin2‘); 126 res.end(‘haha9‘); 127 } 128 129 } */
五、本章总结代码:
1 const event =require(‘events‘); 2 const eventEmitter = new event.EventEmitter(); 3 function func(){ 4 console.log(‘func执行‘); 5 } 6 function func2(){ 7 console.log(‘function2 执行‘); 8 } 9 eventEmitter.on(‘eventEmitterName‘,func); 10 eventEmitter.addListener(‘eventEmitterName‘,func2); 11 let listers = require(‘events‘).EventEmitter.listenerCount(eventEmitter,‘eventEmitterName‘); 12 console.log(‘当前连接数:‘+listers); 13 eventEmitter.emit(‘eventEmitterName‘); 14 15 eventEmitter.removeListener(‘eventEmitterName‘, func); 16 17 console.log(‘---------------------------------‘); 18 console.log(‘func连接取消‘); 19 eventEmitter.emit(‘eventEmitterName‘); 20 listers = require(‘events‘).EventEmitter.listenerCount(eventEmitter, ‘eventEmitterName‘); 21 console.log(‘当前连接数:‘ + listers); 22 console.log(‘-------------------------------‘); 23 eventEmitter.removeListener(‘eventEmitterName‘,func2); 24 listers = require(‘events‘).EventEmitter.listenerCount(eventEmitter, ‘eventEmitterName‘); 25 console.log(‘当前连接数:‘ + listers); 26 eventEmitter.emit(‘eventEmitterName‘); 27 console.log(‘------------------------------‘); 28 29 30 31 32 eventEmitter.on(‘eventEmitterName‘,func); 33 34 listers = require(‘events‘).EventEmitter.listenerCount(eventEmitter, ‘eventEmitterName‘); 35 console.log(‘当前连接数:‘ + listers); 36 eventEmitter.emit(‘eventEmitterName‘); 37 38 console.log(‘______________________‘);