Node.js高级编程读书笔记 - 1 基本概念
Outline
1 概述和安装
- 1.1 安装Node
- 1.2 Node简介
2 Node核心API基础
- 2.1 加载模块
- 2.2 应用缓冲区处理、编码和解码二进制数据
- 2.3 使用时间发射器模式简化事件绑定
- 2.4 使用定时器制定函数执行计划
1 概述和安装
1.1 安装Node
参见Ubuntu 14.04下搭建Node.js的开发环境或其他类似文章。
npm: Node包管理器
两种主要的工作模式:全局模式、本地模式
本地模式:安装到当前目录下node_modules目录中。
常用命令:
npm install [-g] <package name>
npm install <package name>@<version specification>
npm install sax@0.2.5
npm install sax@"<0.3"
npm install sax@">=0.1.0 <0.3.1"
npm uninstall <package name>
npm update [-g] <package name>
用pacakge.json定义依赖关系
// MYAPP/package.json
{
"name":"MyApp",
"version":"1.0.1",
"dependencies":{
"sax":"0.3.x",
"nano":"*"
}
}
1.2 Node简介
事件驱动编程风格
一种编程风格,程序的执行流程取决于事件。事件由事件处理程序或者事件回调函数处理。
事件驱动编程风格中一个重要的概念:事件循环。事件循环:处于不断循环的一个结构,负责处理事件检测和事件触发处理。
在每一轮事件循环中,需要检测发生了什么事情,依据发生的事件决定调用哪个事件处理程序/事件回调函数。
闭包
本质:函数作为一等公民(first class citizen)
特性:可以记住其自身被声明的作用域(父上下文)中的变量,即使运行时父上下文已不复存在。
2 Node核心API基础
2.1 加载模块
Node.js实现了CommonJS模块标准,为每个模块赋予一个上下文,将模块隔离开来。
//1 加载核心模块
var http = require("http");
//2 加载文件模块
var myModule = require("./myModule");
// myModule.js
function doSomething(){
console.log("do something");
}
module.exports = doSomething;
//3 加载文件夹模块
var myModule = require("./myModule");
//3.1 myModule文件夹下存在package.json
{
"name":"myModule",
"main":"./lib/myModule.js" //入口点
}
//3.2 myModule文件夹下不存在pakcage.json,存在index.js
// 则index.js作为入口点
//4 从node_modules文件夹中加载
var myModule = require("myModule.js")
//CAUTION: 会首先尝试加载./node_modules/myModule.js,不存在则在上一级文件夹中查找,直到根文件
注意项:模块只会被加载一次,在首次加载时被缓存,无运行时切换机制。处理模块加载时副作用时需要特别注意。
2.2 应用缓冲区处理、编码和解码二进制数据
/**
demonstration of Buffer usage
*/
var buf1 = new Buffer("Hello World");
console.log(buf1.toString());
// convet to base64
console.log(buf1.toString("base64"));
//second parameter's candidate value: ascii, utf8, base64
var buf2 = new Buffer("SGVsbG8gV29ybGQ=", "base64");
console.log(buf2.toString("base64"));
console.log(buf1.toString());
// CAUTION: not initialized, i.e. all zero
var buf3 = new Buffer(1024);
console.log(buf3.toString());
console.log(buf3.length);
// content getter/setter
console.log(buf1[1]);// e: 101
buf1[99] = 101;// not changed
console.log(buf1.toString());
// slice: share the underling data structure
var buffer = new Buffer("What is ration is real, and what is real is rational.");
var partOfBuffer = buffer.slice(0, 15);
console.log(partOfBuffer.toString());
partOfBuffer[0] = 119;// w: 119
console.log(buffer.toString());
console.log(partOfBuffer.toString());
// copy: different underling data structure
var content = "What is ration is real, and what is real is rational.";
var buffer1 = new Buffer(content);
var targetLength = 11;
var buffer2 = new Buffer(targetLength);
var targetStart = 0;
var sourceStart = 1;
var sourceEnd = 12;// sourceStart + targetLength
buffer1.copy(buffer2, targetStart, sourceStart, sourceEnd);
console.log(buffer2.toString());
2.3 使用时间发射器模式简化事件绑定
CPS: 连续传递风格(continuation passing style)
在连续传递风格中,每个函数在执行完毕后,都会调用一个回调函数,使得程序能够继续运行。
sample:
/**
demonstration of CPS(continuation passing style)
*/
// read password
var fs = require("fs"); // see 3.1 查询和读写文件
fs.readFile("/etc/passwd", function(err, fileContent){
if(err){
throw err;
}
console.log("file content: ", fileContent.toString());
});
事件发射器模式(event emitter pattern)
应对问题:函数执行过程中出现多个事件、或者某一事件多次出现,需要执行相应的事件处理器和以可编程的方式“记录”事件和事件处理器之间的对应关系。
事件发射器模式的两个对象
- 事件发射器(event emitter):可以发射事件的对象
- 事件监听器(event listener):绑定到事件发射器上的代码,负责监听特定类型的事件
API
/**
demonstration of event emitter API, including:
- addListener/on
- once
- removetListener
- removeAllListeners,
and anyone object implemented event emitter pattern has all these methods
*/
//文件流,see 3.3 读写数据流
// read stream is an object implemented event emitter pattern
var fs = require("fs");
var path = "event_emitter_api.js";// current file
var readStream = fs.createReadStream(path);
// 1 addListener
function receiveData(data){
console.log("receiveData: got data from file read stream: %s", data.toString().substring(0,10));
}
//readStream.addListener("data", receiveData);
// or use on()
readStream.on("data", receiveData);
// 2 binding more than one event listeners
function receiveData2(data){
console.log("receiveData2: got data from file read stream: %s", data.toString().substring(0,10));
}
readStream.on("data", receiveData2);
// 3 remove event listeners
function receiveData3(data){
console.log("receiveData3: got data from file read stream: %s", data.toString().substring(0,10));
}
readStream.on("data", receiveData3);
readStream.removeListener("data", receiveData3);
// 4 once: one time shot event handler
readStream.once("data", receiveData3);
// 5 removeAllListeners
readStream.removeAllListeners("data");
自定义事件发射器
/**
demonstration of customed event emitter
*/
var util = require("util");
var EventEmitter = require("events").EventEmitter;
var MyEventEmitter = function(){
}
// construct a prototype chain, so MyEventEmitter can use prototype method of the EventEmitter
util.inherits(MyEventEmitter, EventEmitter);
MyEventEmitter.prototype.someMethod = function(){
this.emit("my event", "argument1", "argument2");
}
// usage
var myEventEmitter = new MyEventEmitter();
myEventEmitter.on("my event", function(arg1, arg2){
console.log("got a event with parameters: %s, %s", arg1, arg2);
});
// emit the event every second
function main(myEventEmitter){
setInterval(function(){
myEventEmitter.someMethod();
}, 1000);
}
main(myEventEmitter);
2.4 使用定时器制定函数执行计划
3 main constructors:
- setTimeout()
- setInterval()
- process.nextTick()
sample code:
/**
demonstration of add constraint to event handle sequences
*/
// 1 delay to handle in the next round event loop
process.nextTick(function(){
console.log("I should be executed in next event loop");
});
// 2 blocking the event loop: so act well to follow the rules
/*
process.nextTick(function(){
while(true){
console.log(".");
}
});
process.nextTick(function(){
console.log("I should never be executed.");
});
setTimeout(function(){
console.log("now timeout");
}, 1000);
*/
// 3 sequence all executions
function myAsyncFunction(func){
setTimeout(function(){
func();
}, 1500);
}
(function sequence(){
setTimeout(function do_it(){
myAsyncFunction(function(){
console.log("async is done now");
sequence();
});
}, 1000);
}()); // execute immediately
Node.js高级编程读书笔记 - 1 基本概念的更多相关文章
- Node.js高级编程读书笔记Outline
Motivation 世俗一把,看看前端的JavaScript究竟能做什么. 顺便检验一下自己的学习能力. Audience 想看偏后台的Java程序员关于前端JavaScript的认识的职业前端工程 ...
- Node.js高级编程读书笔记 - 6 应用程序构建和调试 - Never
Explanation 现阶段console.log(...),util.inspect(...), JSON.stringify(...)在控制台输出已经够用了[2015/07/19]. 单元测试隶 ...
- Node.js高级编程读书笔记 - 4 构建Web应用程序
Outline 5 构建Web应用程序 5.1 构建和使用HTTP中间件 5.2 用Express.js创建Web应用程序 5.3 使用Socket.IO创建通用的实时Web应用程序 5 构建Web应 ...
- Node.js高级编程读书笔记 - 3 网络编程
Outline 3.4 构建TCP服务器 3.5 构建HTTP服务器 3.6 构建TCP客户端 3.7 创建HTTP请求 3.8 使用UDP 3.9 用TLS/SSL保证服务器的安全性 3.10 用H ...
- Node.js高级编程读书笔记 - 2 文件和进程处理
Outline 3 文件.进程.流和网络 3.1 查询和读写文件 3.2 创建和控制外部进程 3.3 读写数据流 3 文件.进程.流和网络 3.1 查询和读写文件 path 从Node 0.8起,pa ...
- Node.js高级编程读书笔记 - 5 数据库 - Never
Outline 6 连接数据库 6.1 使用node-mysql连接MySQL数据库 6.2 使用Nano连接CouchDB数据库 6.3 使用Mongoose连接MongoDB数据库 6 连接数据库 ...
- JS高级编程读书笔记
导读:由于书的内容较多,内容划分也非常详尽,所以会分好几篇来写. 此页面仅作为跳转,权当个目录来用. 我会分块进行整理,大致如下: 第一章 简介 讲述javascript的历史,不打算整理,同学们大概 ...
- 《Node.js 高级编程》简介与第二章笔记
<Node.js 高级编程> 作者简介 Pedro Teixerra 高产,开源项目程序员 Node 社区活跃成员,Node公司的创始人之一. 10岁开始编程,Visual Basic.C ...
- JavaScript、jQuery、HTML5、Node.js实例大全-读书笔记3
技术很多,例子很多,只好慢慢学,慢慢实践!!现在学的这本书是[JavaScript实战----JavaScript.jQuery.HTML5.Node.js实例大全] JavaScript.jQuer ...
随机推荐
- Java中request请求之 - 带文件上传的form表单
常用系统开发中总免不了显示图片,保存一些文件资料等操作. 这些操作的背后,就是程序员最熟悉的 enctype="multipart/form-data"类型的表单. 说起file类 ...
- jQuery EasyUI教程之datagrid应用(一)
最近一段时间都在做人事系统的项目,主要用到了EasyUI,数据库操作,然后抽点时间整理一下EasyUI的内容. 这里我们就以一个简洁的电话簿软件为基础,具体地说一下datagrid应用吧 datagr ...
- QT mainwindow四件套
最近在学习QT.下面总结一下mainwindow的设置步骤. 使用的平台为vs2013+qt5.3.2+qt-vs-addin1.2.3 1)安装软件 首先安装vs2013,这个不多介绍. 然后安装q ...
- 焦点问题onfocus=”this.blur()”代替方法(转)
为了去除链接的虚线框,网上搜索到最常见的方法是onfocus=“this.blur()”,不过同时搜索到的是这会不利于盲人浏览使用页面 在淘宝ued官方博客上详细说明了解决方法,这里转了部分,完整版: ...
- php短路与 短路或
关于php短路的问题特性,三种写法的区别 $a = 1;$b=0;第一种: $a && $b = 'cccccccc';第二种 $a || $b = 'cccccccc';第三种 if ...
- jquery的colorbox关闭并传递数据到父窗
function closebox(para1, para2) { var k = parent;// 父窗口对象 k.document.getElementById("para1" ...
- 【接口测试】Jenkins+Ant+Jmeter搭建持续集成的接口测试平台
参考文档: http://www.cnblogs.com/liuqi/p/5224579.html
- ABAP之声母韵母
我们一开始上学的时候,老师最先教的是什么? 拼音,声母,韵母,声调等等. 那么ABAP里什么是这些东西呢? 基础的数据类型,已经数据字典里的东西:域,数据元素,结构,视图,表,搜索帮助,锁... 数据 ...
- TabControl控件的DrawItem事件怎么注册
只有DrawMode等于OwnerDrawFixed时,才会在绘制选项卡时发生DrawItem事件tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
- Nginx 支持 CI 框架的配置并禁止使用 ip 访问
#CIserver { listen 80; server_name www.ci.com; index index.php index ...