本書它由 Node社區著名的 Socket.IO作者—— Guillermo Rauch,通過大量的實踐案例撰寫,並由 Node社區非常活躍的開發者—— Goddy Zhao翻譯而成。
本書內容主要由對五大部分的介紹組成: Node核心設計理念、 Node核心模組 API、Web開發、資料庫以及測試。從前到後、由表及里地對使用 Node進行 Web開發的每一個環節都進行了深入的講解,並且最大的特點就是通過大量的實際案例、代碼展示來剖析技術點,講解最佳實踐。
基本介紹
- 書名:了不起的Node.js
- 又名:將JavaScript進行到底
- 作者:勞奇 (Guillermo Rauch)
- 譯者:趙靜
- ISBN:9787121217692
- 頁數:292
- 定價:CNY 79.00
- 出版社:電子工業出版社
- 出版時間:2014-1-1
- 裝幀:平裝
內容簡介,作者簡介,目錄,
內容簡介
本書它由 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做過演講。"
目錄
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
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