今天開始會和大熊君{{bb}}一塊兒着手翻譯node的系列外文書籍,大熊負責翻譯《Node.js IN ACTION》一書,而我暫時負責翻譯這本《Hands-on Node.js》(下載),爲方便理解不採用直譯的方式,如有翻譯不當的地方還請不吝賜教,我會及時堪正。javascript
引言java
在09年歐洲JSConf大會上,一位叫作Ryan Dahl的年輕程序員介紹了一項他所負責的項目。該項目結合了谷歌V8 Javascript引擎以及event loop機制,從而可使用JS來建立服務端平臺。該項目跟其它服務端JS平臺不太同樣:全部的I/O基元都是事件驅動的,沒有其它捷徑。借力於強大又簡單的JS,本來基於異步讀寫的應用任務,由複雜變得簡單好實現。node
Dahl演講完畢時會場掌聲不斷,他的項目也取得了空前的發展,日益流行並被普遍採用。git
該項目被命名爲Node.js,時下常被程序員們簡稱爲「Node」。Node提供了純事件驅動的、無阻塞的架構來建立高併發的程序,使用它能讓你輕鬆構造快速又可擴展的網絡服務。程序員
Introduction
At the European JSConf 2009, a young programmer by the name of Ryan Dahl, introduced a project
he had been working on. This project was a platform that combined Google’s V8 JavaScript engine
and an event loop to create a server-side platform programmable in JavaScript. The project took a
different direction from other server-side JavaScript platforms: all I/O primitives were event-driven,
and there was no way around it. Leveraging the power and simplicity of JavaScript, it turned the
difficult task of writing asynchronous applications into an easy one. Since receiving a standing
ovation at the end of his talk, Dahl’s project has been met with unprecedented growth, popularity
and adoption.
The project was named Node.js, now known to developers simply as ‘Node’. Node provides purely
evented, non-blocking infrastructure for building highly concurrent software.
Node allows you to easily construct fast and scalable network services.
爲什麼Node如此忽然地、呈指數倍地流行起來?github
服務端JS其實已經出現多時了,是什麼讓Node平臺如此引人注目?web
環顧以前的那些服務端JS的實現,更着重處理的地方仍是把其它平臺(好比Ruby、PERL、Python)上經常使用的方法遷移到JS中,而JS自己不露鋒芒。Node跳過了這種處理,並表示——「讓咱們使用web上那些可成功驅動事件的編程模塊,來輕鬆建立可擴展的服務,並讓這種方式無可替代、無所不能」。編程
能夠說JS自己爲Node的成功作出了很大的貢獻,但這點不能解釋Node以前的其它服務端項目爲什麼沒有流行起來。雖然JS的確具有了廣泛性,但對此Ryan Dah指出,不像其它服務端中JS所作的嘗試,統一客戶端和服務端的語言並不是Node的首要目標。promise
在我看來,Node成功的主要因素有三個:網絡
1. Node很簡單——使用了事件驅動I/O編程,是最好的I/O編程方式,比起其它現有平臺的實現更易懂、更成功。
2. Node很精練——它不會試着去解決所有問題,而是依靠清晰明確的功能性API爲處理internet協議奠基了基礎和支持。
3.Node不妥協——不會試着去兼容以前的軟件,並讓人們從新審視並首肯了其定位。
Why the sudden, exponential popularity?
Server-side JavaScript has been around for some time, what makes this platform so appealing?
In previous server-side JavaScript implementations, javascript was the raison d’etre, and the
approach focussed on translating common practices from other platforms like Ruby, PERL and
Python, into JavaScript. Node took a leap from this and said: 「Let’s use the successful event-driven
programming model of the web and use it to make an easy way to build scalable servers. And let’s
make it the only way people can do anything on this platform.」.
It can be argued that JavaScript itself contributed to much of Node’s success, but that would not
explain why other the server-side projects proceeding Node have not yet come close in popularity.
The ubiquity of JavaScript surely has played a role, but, as Ryan Dahl points out, unlike other Serverside
JavaScript attempts, unifying the client and server into a common language was not the primary
goal for Node.
In my perspective there are three factors contributing to Node’s success:
1. Node is Easy - Node makes event-driven I/O programming, the best way to do I/O programming,
much easier to understand and achieve than in any other existing platform.
2. Node is Lean - Node does not try to solve all problems. It lays the foundation and supports
the basic internet protocols using clean, functional APIs.
3. Node does not Compromise - Node does not try to be compatible with pre-existing software,
it takes a fresh look at what many believe is the right direction.
本書涵蓋內容
咱們會分析爲什麼Node不走尋常路,以及爲什麼你應該使用Node、如何使用Node。咱們會從一個概述開始,而後很快轉入模塊化代碼。在最終你將懂得建立和測試你本身的Node模塊、服務端/客戶端應用,並享受Node的便捷和各類API。
What does this book cover?
We will analyze what makes Node a different proposal to other server-side solutions, why you
should use it, and how to get started. We will start with an overview but quickly dive into some
code module-by-module. By the end of this book you should be able to build and test your own
Node modules, service producers/consumers and feel comfortable using Node’s conventions and
API.
本書沒有涵蓋的內容
本書並不打算介紹徹底部的Node API,但咱們會介紹那些做者認爲的大部分應用所須要的、經常使用的部分。
本書也不會涉及任何Node框架。Node是一個很棒的建立框架的工具,其中也不乏好用的框架,好比簇管理的、跨進程通信的、web框架、網絡流量整合工具、遊戲引擎等等。不過在你開始接觸框架以前,你應該先熟悉Node的基礎架構並瞭解它爲這些構建模塊提供了些什麼。
What does this book not cover?
This book does not attempt to cover the complete Node API. Instead, we will cover what the author
thinks is required to build most applications he would build on Node.
This book does not cover any Node frameworks; Node is a great tool for building frameworks and
many are available, such as cluster management, inter-process communication, web frameworks,
network traffic collection tools, game engines and many others. Before you dive into any of those
you should be familiar with Node’s infrastructure and what it provides to these building blocks.
閱讀本書的先決條件
本書假設你以前並未瞭解任何Node知識,但各類例子的代碼畢竟是用JS書寫的,因此熟悉JS這門語言有助你理解。
Prerequisites
This book does not assume you have any prior knowledge of Node, but the code examples are written
in JavaScript, so familiarity with the JavaScript language will help.
練習
本書在部分小節會有練習題,你能夠在書的最後找到這些練習的答案,但我建議你先自行動手解題,棘手處可查閱本書或訪問http://nodejs.org查閱完整的API文檔。
Exercises
This book has exercises in some chapters. At the end of this book you can find the exercise
solutions, but I advise you to try do them yourself. Consult this book or use the comprehensive
API documentation on the official http://nodejs.org¹ website.
源碼
你能夠在GitHub上找到本書的部分源碼和練習:
https://github.com/pgte/handson_nodejs_source_code
或者直接下載它們:
https://github.com/pgte/handson_nodejs_source_code/zipball/master
Source code
You can find some of the source code and exercises used in this book on GitHub:
https://github.com/pgte/handson_nodejs_source_code²
or you can download it directly:
https://github.com/pgte/handson_nodejs_source_code/zipball/master³
本書的引導方向
在閱讀完本書後,你將瞭解Node API並能夠從事研究那些基於Node所搭建出來的東西(好比適配器、框架和模塊),讓咱們啓程吧。
Where will this book lead you?
By the end of it, you should understand the Node API and be able to pursue the exploration of other
things built on top of it, being adaptors, frameworks and modules.
Let’s get started!