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 基本概念的更多相关文章

  1. Node.js高级编程读书笔记Outline

    Motivation 世俗一把,看看前端的JavaScript究竟能做什么. 顺便检验一下自己的学习能力. Audience 想看偏后台的Java程序员关于前端JavaScript的认识的职业前端工程 ...

  2. Node.js高级编程读书笔记 - 6 应用程序构建和调试 - Never

    Explanation 现阶段console.log(...),util.inspect(...), JSON.stringify(...)在控制台输出已经够用了[2015/07/19]. 单元测试隶 ...

  3. Node.js高级编程读书笔记 - 4 构建Web应用程序

    Outline 5 构建Web应用程序 5.1 构建和使用HTTP中间件 5.2 用Express.js创建Web应用程序 5.3 使用Socket.IO创建通用的实时Web应用程序 5 构建Web应 ...

  4. 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 ...

  5. Node.js高级编程读书笔记 - 2 文件和进程处理

    Outline 3 文件.进程.流和网络 3.1 查询和读写文件 3.2 创建和控制外部进程 3.3 读写数据流 3 文件.进程.流和网络 3.1 查询和读写文件 path 从Node 0.8起,pa ...

  6. Node.js高级编程读书笔记 - 5 数据库 - Never

    Outline 6 连接数据库 6.1 使用node-mysql连接MySQL数据库 6.2 使用Nano连接CouchDB数据库 6.3 使用Mongoose连接MongoDB数据库 6 连接数据库 ...

  7. JS高级编程读书笔记

    导读:由于书的内容较多,内容划分也非常详尽,所以会分好几篇来写. 此页面仅作为跳转,权当个目录来用. 我会分块进行整理,大致如下: 第一章 简介 讲述javascript的历史,不打算整理,同学们大概 ...

  8. 《Node.js 高级编程》简介与第二章笔记

    <Node.js 高级编程> 作者简介 Pedro Teixerra 高产,开源项目程序员 Node 社区活跃成员,Node公司的创始人之一. 10岁开始编程,Visual Basic.C ...

  9. JavaScript、jQuery、HTML5、Node.js实例大全-读书笔记3

    技术很多,例子很多,只好慢慢学,慢慢实践!!现在学的这本书是[JavaScript实战----JavaScript.jQuery.HTML5.Node.js实例大全] JavaScript.jQuer ...

随机推荐

  1. jquery.validate使用 - 1

    jquery.validate使用攻略 好几年不写JS了,资料整理起来比较慢,格式也有点乱 主要分几部分jquery.validate 基本用法jquery.validate API说明jquery. ...

  2. 遗传算法在JobShop中的应用研究(part3:交叉)

    2.交叉 交叉是遗传算法中的一个重要操作,它的目的是从两条染色体中各自取出一部分来组合成一条新的染色体这里,在车间调度中一种常见的交叉方法叫Generalized Order Crossover方法( ...

  3. java内部类以及匿名类

    内部类 一个类内部定义的类称为内部类. 内部类允许把逻辑相关的类组织在一起,并控制内部代码的可视性. 内部类与外部类的结构层次如下. 顶层类:最外层的类 外部类:内部类所在的类 内部类:类内部定义的类 ...

  4. C#程序设计---->计算圆面积windows程序

    值得说的就是添加一个回车事件, http://blog.csdn.net/nanwang314/article/details/6176604 private void textBox1_KeyDow ...

  5. 深入浅出设计模式——抽象工厂模式(Abstract Factory)

    模式动机在工厂方法模式中具体工厂负责生产具体的产品,每一个具体工厂对应一种具体产品,工厂方法也具有唯一性,一般情况下,一个具体工厂中只有一个工厂方法或者一组重载的工厂方法.但是有时候我们需要一个工厂可 ...

  6. 和Java相关的一些好文章(不定期更新)

    1.Java 集合类详解 (包括arraylist,linkedlist,vector,stack,hashmap,hashtable,treemap,collection等). 2.Java 理论与 ...

  7. t检验

    例子:以往通过大规模调查已知某地新生儿出生体重为3.30kg.从该地难产儿中随机抽取35名新生儿作为研究样本,平均出生体重为3.42kg,标准差为0.40kg. 问该地难产儿出生体重是否与一般新生儿体 ...

  8. mysql 配置 utf8 依然乱码

    mysql 乱码问题排除方案: 1.检查数据库及数据表是不是utf8字符集 2.查看一下jdbc.properties配置的数据库url 是否配置了characterEncoding=UTF-8或者在 ...

  9. javascript画直线和画圆的方法(非HTML5的方法)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 使用Settings.settings存储用户的个性化配置

    1.在vs中设计要存储的字段,如:zbl,注意范围选择用户 读取: var aa = Properties.Settings.Default.zbl; 写入: Properties.Settings. ...