了不起的Node.js
出版信息
劳奇 (Guillermo Rauch) / 赵静 / 电子工业出版社 / 2014-1 / 79.00元
内容简介
本书是一本经典的 Learning by Doing的书籍。它由 Node社区著名的 Socket.IO作者—— Guillermo Rauch,通过大量的实践案例撰写,并由 Node社区非常活跃的开发者—— Goddy Zhao翻译而成。
本书内容主要由对五大部分的介绍组成: Node核心设计理念、 Node核心模块 API、Web开发、数据库以及测试。从前到后、由表及里地对使用 Node进行 Web开发的每一个环节都进行了深入的讲解,并且最大的特点就是通过大量的实际案例、代码展示来剖析技术点,讲解最佳实践。
作者简介
Guillermo Rauch(旧金山,加利福尼亚州)是一家位于旧金山,为
当地教育提供相关服务的创业公司LearnBoost的CTO和联合创始人。
Rauch还是几个知名Node.js项目的发明者,曾在JSConf和一些
Node.js workshop做过演讲。"
译者介绍:
Goddy Zhao(本名赵静),Successfactors(SAP子公司)软件工程师。毕业于复旦大学,先后在IBM、淘宝工作过,专注于企业级富客户端Web应用的开发,擅长前后端相结合的技术解决方案。曾与人合译过多本前端图书,并曾在沪JS及D2前端技术论坛担任过主持人和演讲嘉宾。个人主页http://goddyzhao.me。
目录
PART Ⅰ 从安装与概念开始
CHAPTER 1 安装............................................... 3
在Windows下安装............................................... 3
在OS.X下安装. ................................................ 4
在Linux下安装................................................. 5
编译. ............................................................5
确保安装成功. ...............................................5
Node.REPL................................................. 5
执行文件.......................................................... 6
NPM................................................................. 6
安装模块...........................................................7
自定义模块. .......................................................8
安装二进制工具包. ..................................................9
浏览NPM仓库. .......................................................9
小结................................................................. 10
CHAPTER 2 JavaScript概览.........................................11
介绍.......................................................... 11
JavaScript基础..................................................... 12
类型. ......................................................... 12
类型的困惑. ............................................... 12
函数. .......................................................... 13
THIS、FUNCTION #CALL以及FUNCTION#APPLY.................... 14
函数的参数数量. ........................... 14
闭包. ........................................................ 14
类. .............................................................. 15
继承. ..................................................... 16
TRY.{}.CATCH.{}...................................... 17
v8中的JavaScript............................... 17
OBJECT#KEYS....................................................... 18
ARRAY#ISARRAY................................................ 18
数组方法................................................. 18
字符串方法.. .................................................... 19
JSON............................................................. 19
FUNCTION#BIND....................................................... 19
FUNCTION#NAME.............................................. 19
_PROTO_(继承). ............................................ 20
存取器....................................................... 20
小结.............................................................. 21
CHAPTER 3 阻塞与非阻塞IO................................. 23
能力越强,责任就越大................................................. 23
阻塞. ............................................................. 25
单线程的世界. ............................................... 27
错误处理..................................................... 29
堆栈追踪................................................ 30
小结............................................................... 32
CHAPTER 4 Node中的JavaScript ..................................... 33
global对象....................................................... 33
实用的全局对象. ................................................ 34
模块系统............................................................ 34
绝对和相对模块. .................................................. 35
暴露API. ............................................................... 37
事件......................................................... 38
buffer............................................................... 40
小结................................................................... 41
PART Ⅱ Node重要的API
CHAPTER 5 命令行工具(CLI)以及FS API:首个Node应用................... 45
需求................................................. 45
编写首个Node程序...................................... 46
创建模块.............................................. 46
同步还是异步. ............................ 47
理解什么是流(stream)................................... 49
输入和输出. .......................................................... 50
重构. ........................................ 53
用fs进行文件操作. .............................. 55
对CLI一探究竟. ................................................. 56
argv.................................................... 57
工作目录...................................... 57
环境变量........................................... 58
退出. ............................................... 58
信号. .......................................................... 58
ANSI转义码............................................ 59
对fs一探究竟. ................................................. 59
Stream......................................................... 59
监视. .......................................... 60
小结...................................................... 61
CHAPTER 6 TCP ........................................... 63
TCP有哪些特性. ..................................................... 64
面向连接的通信和保证顺序的传递. ........................... 64
面向字节........................................ 65
可靠性............................................... 65
流控制......................................... 65
拥堵控制.............................................. 65
Telnet................................................................ 65
基于TCP的聊天程序............................................... 68
创建模块..................................... 68
理解NET.SERVER.API........................ 68
接收连接....................................... 70
data事件. ...................................................... 71
状态以及记录连接情况. ........................................ 73
圆满完成此程序. ............................................ 75
一个IRC客户端程序.......................................... 77
创建模块.............................................. 77
理解NET#STREAM.API.. ......................................... 78
实现部分IRC协议. .................................................. 78
测试实际的IRC服务器. .................................. 78
小结................................................................. 79
CHAPTER 7 HTTP .................................................. 81
HTTP结构......................................................... 81
头信息. ...................................................... 82
连接.............................................................. 87
一个简单的Web服务器.................................................. 88
创建模块....................................... 88
输出表单.................................................... 88
method和URL. ........................................... 90
数据. .............................................. 92
整合. ................................................ 94
让程序更健壮. ..................................... 95
一个Twitter.Web客户端......................................... 96
创建模块....................................... 96
发送一个简单的HTTP请求. ...................... 97
发送数据......................................... 98
获取推文................................................. 99
superagent来拯救.............................................. 102
使用up重启HTTP服务器..................................... 103
小结............................................. 104
PART Ⅲ Web开发
CHAPTER 8 Connect .............................. 107
使用HTTP构建一个简单的网站. ....................................... 108
通过Connect实现一个简单的网站...................................... 111
中间件. ................................................. 112
书写可重用的中间件. .........................114
static中间件. .............................................119
query中间件. .......................................120
logger中间件................................................... 120
body.parser中间件. .................................122
cookie.........................................................125
会话(session). ......................................126
Redis.session..................................................131
methodOverride中间件. ...........................132
basicAuth中间件. ......................................132
小结.............................................................. 134
CHAPTER 9 Express..................................................135
一个小型Express应用. ...................................... 135
创建模块..............................................136
HTML...........................................................136
SETUP..........................................................137
定义路由............................................137
查询. .................................................140
运行. .....................................................141
设置......................................................... 142
模板引擎...................................................... 143
错误处理........................................................ 144
快捷方法................................................. 144
路由................................................ 146
中间件. .................................................... 148
代码组织策略. ......................................... 149
小结................................................. 151
CHAPTER 10 WebSocket .................. 153
Ajax.............................................. 153
HTML5.WebSocket............................................... 156
一个ECHO示例. ......................................................... 157
初始化项目. .............................................157
建立服务器. ..................................................158
建立客户端. ......................................159
运行示例程序. ...................................160
鼠标光标.......................................... 161
初始化示例程序. .........................................161
建立服务器. ............................................161
建立客户端. .....................................164
运行示例程序. .........................................166
面临一个挑战. ............................................. 166
关闭并不意味着断开连接. .............................166
JSON...........................................167
重连. ...................................................167
广播. ................................................167
WebSocket属于HTML5:早期浏览器不支持. ..........................167
解决方案.................................................167
小结............................................................. 167
CHAPTER 11 Socket.IO.............................................169
传输................................................. 170
断开.VS.关闭. ............................................170
事件. ..........................................170
命名空间..............................................171
聊天程序................................................. 172
初始化程序. .......................................172
构建服务器. ..........................................172
构建客户端. .......................................173
事件和广播. ........................................175
消息接收确认. .................................179
一个轮流做DJ的应用................................ 180
扩展聊天应用. ...........................................181
集成Grooveshark.API. ...............................182
播放歌曲...............................................185
小结.................................................... 190
PART Ⅳ 数据库
CHAPTER 12 MongoDB.......................193
安装.................................................. 195
使用MongoDB:一个用户认证的例子. .................................. 195
构建应用程序. .......................................195
创建Express.App.. ..............................196
连接MongoDB. ...................................200
创建文档......................................201
查找文档..................................203
身份验证中间件. ..................................204
校验.. ..............................................205
原子性..............................................206
安全模式................................206
Mongoose介绍......................................... 206
定义模型........................................207
定义嵌套的键. ..........................208
定义嵌套文档. .....................................209
构建索引..........................................209
中间件.................................................209
探测模型状态.. ...............................210
查询. ..............................................210
扩展查询........................................210
排序.. ........................................211
选择. ......................................................211
限制.. .....................................211
跳过. .........................................211
自动产生键. ...............................211
转换. ..............................................212
一个使用Mongoose的例子........................................ 212
构建应用.................................212
重构. ..............................213
建立模型.................................213
小结.......................................... 215
CHAPTER 13 MySQL...........................................217
node-mysql........................................ 217
初始化项目. .......................................217
Express应用. ...................................218
连接MySQL. .....................................219
初始化脚本. .......................................220
创建数据................................................224
获取数据.....................................228
sequelize............................................ 229
初始化sequelize. ................................230
初始化Express应用. ....................................230
连接sequelize. .........................233
定义模型和同步. ........................... 234
创建数据.................................236
获取数据......................................238
删除数据..............................239
完整地完成应用. ...............................240
小结.......................................... 241
CHAPTER 14 Redis......................................... 243
安装Redis............................................... 244
Redis查询语言. ............................................... 245
数据类型............................................. 245
字符串. ...................................246
哈希. .................................................246
列表. ......................................... 248
数据集. .....................................249
有序数据集. .........................................249
Redis和Node. .......................................... 249
使用node-redis实现一个社交图谱. .............250
小结......................................... 259
PART Ⅴ 测试
CHAPTER 15 代码共享 ................................. 263
什么样的代码可以共享........................................... 263
书写兼容的JavaScript代码................................ 264
导出模块......................................................264
模拟实现ECMA.API. ................................265
模拟实现Node.API. ......................................267
模拟实现浏览器端API. ...............................267
跨浏览器的继承实现. ...........................268
集成到一起:browserbuild. ...................... 268
基础案例....................................269
小结......................................... 271
CHAPTER 16 测试................................... 273
简单测试........................................ 273
测试目标............................................. 274
测试策略................................... 274
测试程序...................................................275
expect.js................................................. 276
API一览. .........................................276
Mocha................................................. 278
测试异步代码. ..................................279
BDD风格. ................................281
TDD风格. .......................................281
export风格. .................................282
在浏览器端使用Mocha. ...................................282
小结.................................... 284
索引 ...................................... 285