没有合适的资源?快使用搜索试试~ 我知道了~
首页《Node.js实战指南》:快速掌握服务器端JavaScript开发
《Node.js实战指南》:快速掌握服务器端JavaScript开发
需积分: 42 10 下载量 102 浏览量
更新于2024-07-17
收藏 5.39MB PDF 举报
"《Node.js:正确的方式》是一本专为Jabezzhang准备的前端开发指南,旨在帮助读者快速入门并深入了解现代服务器端JavaScript编程。作者们如Allen Wirfs-Brock(ECMAScript语言规范项目编辑)推崇它为初学者提供了全面且深入的学习资源,强调这本书是学习Node.js的首选教材。 书中超越了基础的JavaScript和Node.js概念,深入探讨了构建高质量服务器应用程序的实质内容。Rick Waldron(Bocoup, LLC的软件工程师)指出,这不仅仅是一本介绍Node.js作为 hipster 风格Web应用服务器的书籍,而是展现了其灵活性和强大功能,适合任何希望探索服务器基础设施的JavaScript开发者。 Eric Redmond(《七个星期七种数据库》合著者)认可《Node.js:正确的方式》是一本实用性强、内容丰富的读物,它跳过了简单的示例,而是通过实际案例演示Node.js的应用潜力,让读者能够快速领略其在服务器世界中的作用。 最后,来自Baydin, Inc.的Xavi Ramirez也高度评价此书,认为它是学习Node.js的最佳选择,书中直接引入了与实际项目相关的示例,帮助读者跨越“Hello World”阶段,步入更有深度的开发领域。 《Node.js:正确的方式》不仅提供了基础知识,还为开发者提供了丰富的实践经验,是踏上Node.js编程之旅的理想起点,无论你是新手还是寻求提升的开发者,都能从中受益匪浅。"
资源详情
资源推荐
East of the mountain range, we find the client-side programs. These include
GUI tools of all stripes, consumer applications, mobile apps, and web apps.
Client-side programs interact directly with human beings, often by waiting
patiently for their input.
West of the mountains are the server-side programs. This vast expanse is
Node.js territory.
Deep within the server-side region lies the Web—that old guard of HTTP, Ajax,
REST, and JSON. The websites, apps, and APIs that consume so much of
our collective mental energy live here.
Because we spend so much time thinking about the Web, we overemphasize
Node’s use in developing web applications. People ask, “How is Node better
for making web apps?” or, “How can I make a REST service with Node?”
These are good questions, but they miss the point. Node is great for a wider
range of things, and this book explores that larger world.
Node’s Niche
Since JavaScript’s first appearance in 1995, it has been solving problems all
along the front-end/back-end spectrum. The following figure shows this
spectrum and where Node.js fits within it.
Figure 2—Node’s place in the JavaScript spectrum
In the web browser on the right, much of the scripting involves waiting for
user interaction. Click here, drag that, choose a file, etc. JavaScript has been
extraordinarily successful in this space.
Chapter 1. Getting Started • 2
report erratum • discuss
Prepared exclusively for jabez zhang
On the left, back-end databases are investing heavily in JavaScript. Document-
oriented databases like MongoDB and CouchDB use JavaScript extensively
—from modifying records to ad-hoc queries and mapreduce jobs. Other
datastores, like Neo4j and Elasticsearch, present data in JavaScript Object
Notation (JSON). These days, you can even write SQL functions for Postgres
in JavaScript with the right plug-in.
Many middleware tasks are I/O-bound, just like client-side scripting and
databases. These server-side programs often have to wait for things like a
database result, feedback from a third-party web service, or incoming connec-
tion requests. Node.js is designed for exactly these kinds of applications.
How Node Applications Work
Node.js couples JavaScript with an event loop for quickly dispatching opera-
tions when events occur. Many JavaScript environments use an event loop,
but it is a core feature of Node.js.
Node’s philosophy is to give you low-level access to the event loop and to
system resources. Or, in the words of core committer Felix Geisendörfer, in
Node “everything runs in parallel except your code.”
1
If this seems a little backwards to you, don’t worry. The following figure shows
how the event loop works.
Figure 3—The Node.js event loop
1.
http://www.debuggable.com/posts/understanding-node-js:4bd98440-45e4-4a9a-8ef7-0f7ecbdd56cb
report erratum • discuss
How Node Applications Work • 3
Prepared exclusively for jabez zhang
As long as there’s something left to do, Node’s event loop will keep spinning.
Whenever an event occurs, Node invokes any callbacks (event handlers) that
are listening for that event.
As a Node developer, your job is to create the callback functions that get
executed in response to events. Any number of callbacks can respond to any
event, but only one callback function will ever be executing at any time.
Everything else your program might do—like waiting for data from a file or
an incoming HTTP request—is handled by Node, in parallel, behind the scenes.
Your application code will never be executed at the same time as anything
else. It will always have the full attention of Node’s JavaScript engine while
it’s running.
Single-Threaded and Highly Parallel
Other systems try to gain parallelism by running lots of code at the same
time, typically by spawning many threads. But not Node.js. For JavaScript,
Node is a single-threaded environment. At most, only one line of your code
will ever be executing at any time.
Node gets away with this by doing most I/O tasks using nonblocking
techniques. Rather than waiting line-by-line for an operation to finish, you
create a callback function that will be invoked when the operation eventually
succeeds or fails.
Your code should do what it needs to do, then quickly hand control back over
to the event loop so Node can work on something else. We’ll develop practical
examples of this throughout the book, starting in Chapter 2, Wrangling the
File System, on page 9.
If it seems strange to you that Node achieves parallelism by running only one
piece of code at a time, that’s because it is. It’s an example of something I call
a backwardism.
Backwardisms in Node.js
A backwardism is a concept that’s so bizarre that at first it seems completely
backwards. You’ve probably experienced many backwardisms while learning
to program, whether you noticed them or not.
Take the concept of a variable. In algebra it’s common to see equations like
“
7x + 3 = 24
.” Here,
x
is called a variable; it has exactly one value, and your job
is to figure out what that value is.
Chapter 1. Getting Started • 4
report erratum • discuss
Prepared exclusively for jabez zhang
Then when you start learning how to program, you quickly run into statements
like “
x = x + 7
.” Now
x
is still called a variable, but it can have any value that
you assign to it. It can even have different values at different times.
From algebra’s perspective, this is a backwardism. The equation “
x = x + 7
”
makes no sense at all. The notion of a variable in programming is not just a
little different—it’s 100 percent backwards. But once you understand the
concept of assignment, the programming variable makes perfect sense.
So it is with Node’s single-threaded event loop. From a multithreaded perspec-
tive, running just one piece of code at a time seems silly. But once you
understand event-driven programming—with nonblocking APIs—it becomes
clear.
Programming is chock-full of backwardisms like these, and Node.js is no
exception. Starting out, you’ll frequently run into code that looks like it should
work one way, but it actually does something quite different.
That’s OK! With this book, you’ll learn Node by making compact programs
that interact in useful ways. As we run into more of Node’s backwardisms,
we’ll dive in and explore them.
Aspects of Node.js Development
Node.js is a surprisingly big subject, so let’s break it down into different
aspects. There are many aspects of Node.js development we might talk about,
ranging from basic JavaScript syntax to revision control. This book focuses
on five in particular:
• Practical programming
• Architecture and core
• Patterns
• JavaScriptisms
• Supporting code
Let’s explore each of these briefly.
Practical Programming
Practical programming is all about producing real code that does something
useful. Interacting with a file system, establishing socket connections, or
serving web applications are all examples of practical programming.
Each of the remaining chapters of this book focuses on one particular practical
domain. Through code examples specific to each domain, you’ll learn Node’s
architecture, patterns, JavaScriptisms, and supporting code.
report erratum • discuss
Aspects of Node.js Development • 5
Prepared exclusively for jabez zhang
Architecture and Core
Understanding Node’s architecture will help you to harness its features while
avoiding performance-crushing pitfalls. For example, Node uses an event loop
written in C for scheduling work. But it executes application code in a Java-
Script environment. How information is shuttled between these layers is the
kind of impactful architectural detail you’ll learn.
Patterns
Like any successful codebase with a healthy ecosystem, Node.js has a number
of repeating patterns. Some of these patterns are baked into the core while
others mostly appear in third-party libraries. Examples include the use of
callbacks, error-handling techniques, and classes like
EventEmitter
and
Stream
,
which are used liberally for event dispatching.
As we progress through different practical programming domains, we’ll natu-
rally encounter these and other patterns. When we do, you’ll discover why
they’re useful and how to use them effectively.
JavaScriptisms
JavaScript is the language of Node programs, so you’ll be seeing quite a lot
of it. The code examples in this book make use of the latest available Java-
Script features. Some of these features may be unfamiliar to you, even if
you’ve done JavaScript development before.
JavaScriptisms discussed in this book include things like the nature of
functions and object inheritance. ECMAScript Harmony—the code name for
the next version of the JavaScript spec—packs some new features we’ll use,
too.
Supporting Code
Code does not live in isolation; it takes a village to support any individual
program. Supporting code covers lots of things, from unit testing to perfor-
mance benchmarks to deployment scripts. We’ll use supporting code
throughout the book to make our programs more robust, more scalable, and
more manageable.
With these five aspects, you’ll be able to develop applications that make the
most use out of the platform while using idiomatic Node.js style. The example
applications you’ll develop in this book are functional and small, and aim to
clearly demonstrate the five aspects. But to use them, you’ll need to get Node.js
installed first.
Chapter 1. Getting Started • 6
report erratum • discuss
Prepared exclusively for jabez zhang
剩余142页未读,继续阅读
house.zhang
- 粉丝: 35
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 构建Cadence PSpice仿真模型库教程
- VMware 10.0安装指南:步骤详解与网络、文件共享解决方案
- 中国互联网20周年必读:影响行业的100本经典书籍
- SQL Server 2000 Analysis Services的经典MDX查询示例
- VC6.0 MFC操作Excel教程:亲测Win7下的应用与保存技巧
- 使用Python NetworkX处理网络图
- 科技驱动:计算机控制技术的革新与应用
- MF-1型机器人硬件与robobasic编程详解
- ADC性能指标解析:超越位数、SNR和谐波
- 通用示波器改造为逻辑分析仪:0-1字符显示与电路设计
- C++实现TCP控制台客户端
- SOA架构下ESB在卷烟厂的信息整合与决策支持
- 三维人脸识别:技术进展与应用解析
- 单张人脸图像的眼镜边框自动去除方法
- C语言绘制图形:余弦曲线与正弦函数示例
- Matlab 文件操作入门:fopen、fclose、fprintf、fscanf 等函数使用详解
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功