Node.js的基础知识(一)
一、Buffer类
1.创建缓冲区的三种方式
var buffer = new Buffer(10); console.log(buffer); var buffer2 = new Buffer([10,20,30]); console.log(buffer2); var buffer3 = new Buffer('hello henanqingyun','utf-8'); console.log(buffer3);
2.缓冲区切片
console.log(buffer3.toString()); buffer2.fill();//缓冲区清零 buffer2 = buffer3.slice(3,5); console.log(buffer2.toString()); console.log(buffer2.toJSON());
3.复制缓冲区
var buffer4 = new Buffer(30); buffer4.fill(); //这个是对buffer4的清零 buffer3.copy(buffer4); console.log(buffer4.toString());
4.stringDecoder其作用相当于toString()方法
var stringDecoder = require('string_decoder').StringDecoder; var decoder = new stringDecoder(); console.log(decoder.write(buffer3));
5.缓冲区的拼接concat
var buffer = new Buffer('hqingyu '); var buffer2 = new Buffer('node.js'); console.log(Buffer.concat([buffer,buffer2]).toString());
二、流的概念 Stream
1.可读流,可以读取相应的文档,并显示出来
var fs = require('fs'); var data = ''; //创建一个读取流,读取的目标是.txt var readStream = fs.createReadStream('test.txt'); //添加data事件,会反复的读取,并将读取到的chunk参数赋值给data,直到全部读完 readStream.on('data',function(chunk){ data += chunk; }); readStream.on('end',function(){ console.log(data);
});
2.可写流可以将数据data写入到指定的txt文档中,例子如下:
var fs = require('fs'); var data = '这是一段用writeable流写入的数据'; var writeStream = fs.createWriteStream('test.txt'); writeStream.write(data); writeStream.end(); writeStream.on('finish',function(){ console.log('写入完成'); }); writeStream.on('error',function(){ console.log(error.stack); }); console.log('程序执行完毕');
3.管道流,优点是将前面两种方法结合,读的同时,再写。像是在一个管道中
var fs = require('fs'); var readStream = fs.createReadStream('test.txt'); var writeStrame = fs.createWriteStream('destination.txt'); readStream.pipe(writeStrame); console.log('program is end');
三、事件监听
1.基于http的事件监听(最基本的响应网页请求)
var http = require('http'); var server = http.createServer(); server.on('request',function(req,res){ res.end('hello henann'); }).listen(8000);
2. 手动触发监听事件
var events = require('events'); var eventEmitter = new events.EventEmitter(); eventEmitter.on('connection',function(){ console.log('链接可以'); eventEmitter.emit('data_recived'); }); eventEmitter.on('data_recived',function(){ console.log('数据接收'); }); eventEmitter.emit('connection'); console.log('程序完成');
3.addListeners这个可以设计自定义的事件监听;emitter.removeListener();移除一个事件;emitter.removeAllListeners();移除多个事件emitter.listenerCount();获得事件的次数;
例子(
*定义一个自定义的类,账户 account
* 初始金额 =0
* 两个方法,存取钱
* 三个外部函数:通知存款余额
* 当存款小于0
* 当存款大于1000时触发
)如下:
var events = require('events'); function MyAccount(){ this.money = 0; this.drawmoney = function (account) { this.money -= account; this.emit('change'); } this.savemoney = function (account) { this.money += account; this.emit('change'); } } MyAccount.prototype = new events.EventEmitter(); function message(){ console.log('账户还剩',this.money); } function less(){ if(this.money<0){ console.log('请存钱!'); } } function more(account,goal){ if (account.money>goal){ console.log('钱真多'); } } var account = new MyAccount(); /* * 证明继承成功 * */ //console.log(account.constructor.prototype); account.on('change',message); account.on('change',less); account.on('change',function(){ more(this,1000); }); account.savemoney(200); account.savemoney(500); account.savemoney(301); account.drawmoney(1200);