《Node.js 8开发实战》前言

2018-01-24

Preface

前言

In recent years, two big shifts have happened in the practice of writing software—and Node.js has been at the forefront of both.
近年来,在软件编程实战中发生了两大转变。Node.js 在这两方面都处于领先位置。
First, software is becoming increasingly asynchronous. Whether you’re waiting
on a Big Data job, interacting with end users, steering a quadcopter, or simply
responding to an API call, chances are you’ll need asynchronous programming
techniques.
首先,软件变得越来越异步。不论你是在寻求一份大数据的工作,和终端用户做交互,抑或是需要操纵无人机,甚至只是一个简单的对API调用的响应,你都可能会需要用到异步编程。

Second, JavaScript has quietly become the world’s standard code-execution
environment. It’s everywhere: in web browsers, modern NoSQL databases,
DIY robots, and now on the server as well.
其次,JavaScript已悄然编程了世界公认的优秀代码执行环境。他可以运行在浏览器、时髦的NoSQl数据库、DIY机器人,现在也能跑在服务端,可谓无处不在。

Node.js is an integral part of these trends, and it has taken off in a big way.
Node.js是这些趋势中不可分割的一部分,并且已经取得了长足的发展。

Why Node.js the Right Way?

为什么写这本书?

Way back in March of 2010, I gave a lightning talk titled “Full-Stack Java-
Script” at the NoSQL Boston conference. Back then, and even more so now,
I knew that using JavaScript for every layer of the application stack was not
only possible, but was a great way to reduce software complexity.
时间回到2010年5月份,在波士顿的NoSQL大会上,我做了一个题为“JavaScript全栈开发”的简短演讲。当时,我就知道在应用框架的每个层级使用JavaScript不仅是一种可能,更是优化软件复杂度一种优秀方案。如今我对此的理解更加深刻。

When each layer of your stack speaks JavaScript, you sidestep impedance
mismatches and facilitate code reuse. Node.js is an important piece of the
puzzle, filling the middle space between your front-end user-facing code and
your data-storage layer.
当每个你的应用的每个层级都是用JavaScript,你就可以避免阻抗不匹配(译者:应用程序和数据库表示信息的模式之间的不匹配),同时也增强代码可重用性了。Node.js 是填补前端展示层与数据存储层之间空缺的重要一环。

The Right Way in this book’s title refers to both the process of learning Node.js
and the practice of writing Node.js code.
这本书的标题里的“实战”这个词表明这本书既有学习Node.js的过程也有代码实操的练习。

Learning Node.js

学Node.js

As with any growing technology, there are plenty of resources available for
learning Node.js. Unfortunately, many of those resources are narrowly focused
on serving up web resources.
和其他任何一种成长中的技术一样,你可以找到很多学习Node.js的资源。但是,这些资源大部分局限在web服务上。

The web is great, but it’s not enough, and it’s not the whole story of Node.js.
Node.js is about more than just serving web apps, and this book treats it
that way.
web服务很重要,但是还不够,它不是Node.js的全部。Node.js 不单单只是来写web服务应用的,这本书通篇也在贯彻这个理念。
Node.js 8 the Right Way teaches you the concepts you’ll need to be an effective
Node.js programmer, no matter what kinds of programs you need to write.
《Node.js 8开发实战》教给你的概念,能让你高效得开发出各种类型的程序。

Writing Node.js

写Node.js

One thing I love about JavaScript is that there are seven ways to do anything.
There’s breathing room, where developers can explore and experiment and
find better approaches to everything.
我钟情于JavaScript的一个原因是它可以用很多种方法做任何事情,还能留给开发者喘息的空间,从而可以探索和实验并找出最佳解决方案。

The community of Node.js developers, the conventions in Node.js development,
and even the semantics of the JavaScript language itself are all rapidly
evolving. With eyes to the near future, the code examples and recommendations
in this book reflect current best practices and standards.
在Node.js开发者社区里,Node.js的开发规约,甚至于JavaScript语言本身的语义都会很快的在这里得到进化。着眼于未来,这本书里的一些代码例子和建议反映的只是当前的最佳实践和标准。

What’s in This Book

本书的主要内容

This book is for intermediate to advanced developers who want to learn how
to write asynchronous JavaScript for the server using Node.js. Some prior
JavaScript experience will definitely help, but you don’t have to be an expert.
The book proceeds in three parts, outlined here briefly.
这本书适合于想要学习如何用Node.js 为服务器编写异步的JavaScript的中级到高级开发者。你不需要有JavaScript经验,当然如果你有就经验肯定会有帮助。本书分为三部分,下面简要概述。

Part I: Getting Up To Speed on Node.js 8

第一部分 快速学习Node.js 8

Part I is about getting you up to speed on Node.js 8. You’ll write Node.js programs that use core modules—and a few external modules as well—to do
things like interact with the filesystem, spin up a cluster of worker processes, and manage network connections.
第一部分会让快速的了解Node.js 8。你将会接触到使用一些核心库以及第三方库来写Node.js,比如:操作文件系统、创建工作进程和管理网络连接。

Getting Started

起步

Chapter 1, Getting Started, on page 3, introduces the Node.js event loop,
explaining how it empowers Node.js to be highly parallel and single-threaded
at the same time. This chapter also outlines the five aspects of Node.js development that frame each subsequent chapter and has some brief instructions on getting Node.js installed on your machine.
第1章, 起步, 第3页 这章介绍Node.js 的事件轮询机制,同时解释了Node.js是怎么在单线程的情况下实现高并发操作的。另外本章还概述了接下来每个章节中会用到的5个知识点,最后简要介绍了如何在你的机器上安装Node.js。

Wrangling the File System

操作文件系统

In Chapter 2, Wrangling the File System, on page 11, you’ll start writing Node.js programs. If you’ve done any server-side programming in the past, chances are you’ve had to access a filesystem along the way. We’ll start in this familiar domain, using Node.js’s filesystem tools to create asynchronous, nonblocking file utilities. You’ll use Node.js’s ubiquitous EventEmitter and Stream classes to pipe data, and you’ll spawn and interact with child processes.
第2章,操作文件系统,第11页,就将开始写代码啦。如果你之前写过服务端你就会知道我们是不是会碰到需要访问文件系统的时候。我们将以似曾相识的领域作为开始,使用Node.js的文件系统工具创建一个非阻塞的文件操作工具。另外还会使用Node.js无处不在的事件和流来处理管道数据并介绍怎么开启和操作子进程。

Networking with Sockets

创建Socket网络

We’ll expand on those concepts while exploring Node.js’s network I/O capabilities in Chapter 3, Networking with Sockets, on page 27. You’ll create TCP servers and client programs to access them. You’ll also develop a simple
JSON-based protocol and a custom class for working with these messages.
To develop unit tests for the code, you’ll use Mocha, a popular Node.js test
harness.
我们将在第三章,创建Socket网络,第27页展开讲解在探讨Node.js 网络I/O能力时的几个概念。包括创建一个TCP服务器,并用一个客户端去实现TCP访问,还会制定一个简单的基于JSON的消息协议和一个自定义方法来处理消息。为了能够为代码做单元测试,我们将使用到一个比较流行的测试框架Mocha。

Connecting Robust Microservices

使用稳健的微服务

Then, in Chapter 4, Connecting Robust Microservices, on page 53, we’ll branch
away from the Node.js core and into the realm of third-party libraries. You’ll
use npm to import ØMQ (pronounced “Zero-M-Q”)—a high-efficiency, lowlatency
library for developing networked applications. With ØMQ, you’ll
develop programs that communicate using several important patterns, such
as publish/subscribe and request/reply. You’ll create suites of programs that
work together in concert, and you’ll learn the clustering tools to manage them.
接下来,在第4章,使用稳健的微服务,第53页中,我们将抛开Node.js核心来讲讲它的一些第三方库。你将会接触到一个高效率低延迟的npm包0MQ(也叫“Zero-M-Q”)来开发网络应用。通过0MQ,你可以开发出运用发布-订阅、请求-应答等重要设计模式的应用程序。另外还会创建一个协同工作的程序套件,并学会使用集群工具来做协同管理。

Part II: Working with Data

第二部分 和数据打交道

In Part II, you’ll work with real data and lay the groundwork for an end-toend
application. This starts with processing data files in a testable way. You’ll
also learn to compose rich command-line utilities using Node.js and interact
with HTTP services.
在第二部分,你将和真实数据打交道并为端对端的应用打下坚实的基础。这一切从以可测试的方式处理数据文件开始。同时你会学习到如何使用Node.js编写丰富的命令行工具以及和HTTP服务器做数据交互。

Transforming Data and Testing Continuously

数据传输与持续测试

Chapter 5, Transforming Data, on page 81, kicks off an ongoing project that
spans Part II and Part III. You’ll download the catalog from Project Gutenberg,
an online resource for ebooks in the public domain. Using a module called
Cheerio, you’ll write Node.js code to parse the data files and extract the
important fields. You’ll use npm, Mocha, and an assertion library called Chai
to set up continuous testing, and you’ll learn to use Chrome DevTools for
interactive debugging.
第5章,数据传输与持续测试, 第81页我们启动了一个正在开发的项目,将涵盖第二部分和第三部分的内容。你将要下载古腾堡工程(一个文化作品数据化开源项目)的日志数据,并用一个第三方模块Cheerio来解析这些数据文件并输出重要的字段。接着你将使用npm、Mocha和断言库Chai来做持续测试方案,并学会如何使用Chrome开发者工具来交互式的调试程序。

Commanding Databases

数据库操作

In Chapter 6, Commanding Databases, on page 111, you’ll insert the extracted
Project Gutenberg catalog into an Elasticsearch index. To get this done, you’ll
write a command-line utility program called esclu using a Node.js module called Commander. Since Elasticsearch is a RESTful, JSON-based datastore,
you’ll use the Request module to interact with it. You’ll also learn to use a
handy and powerful command-line tool called jq for manipulating JSON.
第6章,数据库操作,第111页,将把为上面抽取出来的古腾堡工程日志数据创建Elasticsearch的索引。要完成这项操作,你讲使用Node.js第三方模块Commander写一个叫esclu的命令行工具。由于Elasticsearch是一个RESTful、基于JSON格式的数据存储对象,你将使用Request模块来做数据交互。同时你还讲学到如何使用一个强大而便捷的命令行工具jq来操作JSON。

Part III: Implementing an Application

第三部分:完整实现一个应用

Part III is where everything comes together. You’ll develop web services that
mediate between your API users and your back-end data services. End users
don’t interact directly with APIs, though, so for that you’ll implement a
beautiful UI. At the end, you’ll tie it all together with session management
and authentication.
第三部分是上面所学的一个汇总。你将开发出一个web服务作为你的API和后端数据服务的沟通的中间层。你也知道不可能让端用户直接操作API,所以你还将完成一套漂亮的UI界面。最终加上session管理和权限验证,将这些服务组装成为一个完整的应用。

Developing RESTful Web Services

开发RESTful Web服务

Node.js has fantastic support for writing HTTP servers, and in Chapter 7,
Developing RESTful Web Services, on page 147, you’ll do exactly that. You’ll
use Express, a popular Node.js web framework for routing requests. We’ll
dive deeper into REST semantics, and you’ll use Promises and async functions
for managing code flows. In addition, you’ll learn to configure your services
using the nconf module, and keep them running with nodemon.
Node.js对于编写HTTP服务支持的非常棒,在第7章,开发RESTful Web服务,第147页中你就将实践这一点。你将用到Express,一个很受欢迎的处理请求服务的web框架。我们将深入REST语义,你将使用Promise和异步函数来管理代码流。另外,你将学到如何使用nconf来配置服务器并使用nodemon来管理服务进程,让它持续运行。

Creating a Beautiful User Experience

构建优雅的用户体验

With the web services in place, in Chapter 8, Creating a Beautiful User Experience, on page 185, you’ll craft a front end for them. You’ll learn how to
assemble a front-end project using a Node.js-based build tool called webpack,
along with a host of peer-dependency plugins for it. You’ll transpile your code
for consumption by the browser using TypeScript, a language and transpiler
from Microsoft that features inferred type checking. To make your UI look
modern and fabulous, you’ll bring in Twitter’s Bootstrap styling framework,
and implement templating with Handlebars.
随着Web服务已经完成,在第八章,构建优雅的用户体验,第185页,你讲给这个服务雕琢一个前端页面。你将学到如何使用webpack以及它的许多依赖插件(一个基于Node.js的构建工具)来构建一个前端项目。你将会用到TypeScript来将你的代码转译成浏览器所认识的代码,TypeScript是微软开发的具有类型检查功能的JS超集语言和转译工具。为了让你的UI界面看起来更加时尚华丽,你将引入Twitter的Bootstrap样式框架,前端模板将使用Handlebar。

Fortifying Your Application

优化你的应用

Chapter 9, Fortifying Your Application, on page 219, is where everything comes
together. You’ll combine the user experience with the web services from the
previous two chapters for an end-to-end solution. Using Express middleware,
you’ll create authenticated APIs and implement stateful sessions. You’ll also
learn how to use npm’s shrinkwrap option to insulate yourself from upstream
module changes.
第9章,优化你的应用,第219页,这里会是一个汇总。你将会把之前两张开发出来端对端解决方案的Web服务和用户界面结合起来。使用Express中间件来创建权限验证API并完成有状态的session管理。同时你也将学会如何使用npm的shrinkwrap现象来隔离上游模块的变化。

Developing Flows with Node-RED

Node-RED的开发模式

After Part III concludes, there’s a special bonus chapter on Node-RED.
Chapter 10, BONUS: Developing Flows with Node-RED, on page 259, walks you
第三部分完结之后,附加了一个关于Node-RED的特别的福利章节[第10章,福利:Node-RED的开发模式,第259页]给你。