NodeJS学习笔记

NodeJS学习笔记

March 13, 2016 10:36 PM

NodeJS核心API

NodeJS的事件模型

注册一个事件

var utils = require('utils'),
EventEmitter = require('events').EventEmitter;
var Server = function() {
	console.log('init');
};
utils.inherits(Server, EventEmitter);
var s = new Server();
s.on('abc', function() {
	console.log('abc');
});

触发一个事件

s.emit('abc');

NodeJS的HTTP模块

http服务器

var http = require('http');
var server = http.createServer();
var handleReq = function(req, res) {
	res.writeHead(200, {});
	res.end('hello world');
};
server.on('request', handleReq);
server.listen(8125);

http客户端

var http = require('http');
var opts = {
	host: 'www.google.com'
	port: 80,
	path: '/',
	method: 'GET'
};
var req = http.request(opts, function(res) {
	console.log(res);
	res.on('data', function(data) {
		console.log(data);
	});
});
req.end();

URL模块
• href
• protocol
• host
• auth
• hostname
• port
• pathname
• search
• query
• hash

NodeJS的IO模块

数据流 stream,创建可读的数据流

var fs = require('fs');
var filehandle = fs.readFile('data.txt', function(err, data) {
	console.log(data)
});

使用缓冲池模型来读取完整的流数据

//stream 是个抽象的数据流
var spool = "";
stream.on('data', function(data) {
	spool += data;
});
stream.on('end', function() {
	console.log(spool);
});

文件系统,利用回调函数

var fs = require('fs');
fs.readFile('warandpeace.txt', function(e, data) {
	console.log('War and Peace: ' + data);
	fs.unlink('warandpeace.txt');
});

用8 位字节数组创建一个3 个字节的Buffer

new Buffer([255,0,149]);

NodeJS工具栏API

DNS

dns.resolve('yahoo.com', 'A', function(e, r) {
	if (e) {
		console.log(e);
	}
	console.log(r);
});

加密

哈希常用于几个重要的功能, 比如把数据混淆以便于验证, 或是为一个较大
的数据提供很小的校验
利用hash生成摘要

> var md5 = crypto.createHash('md5');
> md5.update('foo');
{}
> md5.digest('hex');
'acbd18db4cc2f85cedef654fccc4a4d8'

HMAC 结合了哈希算法和加密密钥,是为了阻止对签名完整性的一些恶意攻击。
公钥加密功能分布在如下4 个类中:Cipher、Decipher、Sign 和Verify。

进程

在Node 退出前调用代码

process.on('exit', function() {
	setTimeout(function() {
		console.log('This will not run');
	}, 100);
	console.log('Bye.');
});

与当前Node 进程交互
操作系统的输入/ 输出,例如通过管道把标准输入转到标准输出

process.stdin.resume();
process.stdin.pipe(process.stdout);

用assert来测试

var assert = require('assert');
assert.equal(1, true, 'Truthy');
assert.notEqual(1, true, 'Truthy');
assert.ok('This is a string', 'Strings that are not empty are truthy');

throws() 和doesNotThrow()

虚拟机

var vm = require('vm');
vm.runInThisContext("1+1");
vm.runInNewContext()

数据库访问

NoSQL数据库

  • CouchDB 提供了JavaScript 环境下基于MVCC1 的文档存储。
  • Redis 是基于内存的key-value 存储,并具备了持久化功能。
  • 因为Mongo 提供了JavaScript 环境下的BSON 对象存储(一种JSON 的二进制变种),因此从Node 去读写数据都非常高效。

SQL数据库

MySQL,node-db 模块提供了常用数据库系统的原生代码接口,包括与MySQL 的接口
PostgreSQL npm install pg

连接池

连接池在Web 开发中是非常重要的概念,因为建立一个数据库连接的开销相对来说还是很大的。为每个请求创建一个甚至多个连接会对高流量网站造成不必要的额外 负担,也会导致性能下降。解决方案是在内部缓存池里维护数据库连接,当某连接不再需要时,它会被放回连接池里,这样就能立刻为下一个进入的请求服务了。

消息队列协议

重要的外部模块

Express

Express(Node 的MVC 框架)也许是使用最广泛的Node 模块了,它吸取了Ruby 的Sinatra 框架的精髓,并提供了许多功能,使得用Node 构建网站变得非常简单。

Socket.IO

Socket.IO 是一个小巧的扩展库,功能很像Node 的核心库net。你可以通过Socket. IO 在浏览器客户端与Node 服务器之间采用高效的底层socket 机制来回发送消息。 该模块的另一个优点是,可以在浏览器与服务器间共享代码。也就是说,一旦你建 立了连接,就可以用同样的JavaScript 代码在两边进行消息通信。

扩展Node

模块

包管理

附件组件

参考

《Node即学即用》

原文地址:https://www.cnblogs.com/gibbonnet/p/5266514.html