0% found this document useful (0 votes)
52 views11 pages

Node - JS: Sa Teach

Record heart rhythms along with your symptoms, lifestyle activities and medications. Clinical studies demonstrated the AliveCor Heart Monitor’s accuracy to be comparable to readings from Lead 1 of standard ECG machines.

Uploaded by

ydh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views11 pages

Node - JS: Sa Teach

Record heart rhythms along with your symptoms, lifestyle activities and medications. Clinical studies demonstrated the AliveCor Heart Monitor’s accuracy to be comparable to readings from Lead 1 of standard ECG machines.

Uploaded by

ydh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

George Ornbo

Sa ms Teach Yourself

Node.js

800 East 96th Street, Indianapolis, Indiana, 46240 USA


Contents at a Glance

Introduction 1

Part I: Getting Started

7
HOUR 1 Introducing Node.js
2 npm (Node Package Manager) 15

3 What Node.js Is Used For 27

4 Callbacks 41

Part II: Basic Websites with Node.js

HOUR 5 HTTP 59

6 Introducing Express 73

7 More 91
on Express
8 Persisting Data 103

Part III: Debugging, Testing, and Deploying

135
HOUR 9 Debugging Node.js Applications
10 Testing Node.js Applications 151

169
11 Deploying Node.js Applications

Part IV: Intermediate Sites with Node.js

189
HOUR 12 Introducting Socket.IO
13 A Socket.IO Chat Server 213

Client 237
14 A Streaming Twitter
15 JSON APIs 265
Table of Contents

Introduction 1

Who Should Read This Book? 1

Why Should I Learn Node.js? 2

How This Book Is Organized 2

Code Examples 2

Conventions Used in This Book 3

Part I: Getting Started

HOUR 1: Introducing Node.js 7

What Is Node.js? 7

What You Can Do with Node.js 8

Installing and Creating Your First Node.js Program 9

Summary 11

Q&A 12

Workshop 12

Exercises 13

HOUR 2: npm (Node Package Manager) 15


What Is npm? 15

Installing npm 16

Installing Modules 17

Using Modules 17

How to Find Modules 19

Local and Global Installation 21

How to Find Module Documentation 22

Specifying Dependencies with package.json 23

Summary 25

Q&A 25

Workshop 26

Exercises 26
vi Sams Teach Yourself Node.js in 24 Hours

HOUR 3: What Node.js Is Used For 27

What Node.js Is Designed to Do 27

Understanding I/O 27

Dealing with Input 29

Networked I/O Is Unpredictable 33

Humans Are Unpredictable 35

Dealing with Unpredictability 37

Summary 38

Q&A 38

Workshop 39

Exercises 39

HOUR 4: Callbacks 41

What Is a Callback? 41

The Anatomy of a Callback 46

How Node.js Uses Callbacks 47

Synchronous and Asynchronous Code 50

The Event Loop 53

Summary 54

Q&A 55

Workshop 55

Exercises 56

Part II: Basic Websites with Node.js

HOUR 5: HTTP 59

What Is HTTP? 59

HTTP Servers with Node.js 59

HTTP Clients with Node.js 69

Summary 70

Q&A 71

Workshop 71

Exercises 72
Contents vii

HOUR 6: Introducing Express 73

What Is Express? 73

Why Use Express? 73

Installing Express 74

Creating a Basic Express Site 74

Exploring Express 76

Introducing Jade 77

Summary 89

Q&A 89

Workshop 90

Exercises 90

HOUR 7: More on Express 91

Routing in Web Applications 91

How Routing Works in Express 91

Adding a GET Route 92

Adding a POST Route 94

Using Parameters in Routes 95

Keeping Routes Maintainable 96

View Rendering 97

Using Local Variables 99

Summary 101

Q&A 101

Workshop 101

Exercises 102

HOUR 8: Persisting Data 103

What Is Persistent Data? 103

Writing Data to a File 104

Reading Data from a File 105

Reading Environment Variables 106

Using Databases 108

Using MongoDB with Node.js 109

Summary 131

Q&A 131
viii Sams Teach Yourself Node.js in 24 Hours

Workshop 132

Exercises 132

Part 111: Debugging, Testing, and Deploying

HOUR 9: Debugging Node.js Applications 135

Debugging 135

The STDIO Module 136

The Node.js Debugger 141

Node Inspector 144

A Note on Testing 147

Summary 148

Q&A 148

Workshop 149

Exercises 149

HOUR 10: Testing Node.js Applications 151

Why Test? 151

The Assert Module 152

Third-Party Testing Tools 155

Behavior Driven Development 159

Summary 167

Q&A 167

Workshop 168

Exercises 168

HOUR 11: Deploying Node.js Applications 169

Ready to Deploy! 169

Hosting in the Cloud 169

Heroku 171

Cloud Foundry 176

Nodester 180

Other PaaS Providers , 184

Summary 184

Q&A 184

Workshop 185

Exercises 186
Contents ix

Part IV: Intermediate Sites with Node.js

HOUR 12: Introducing Socket.lO 189

Now for Something Completely Different 189

Brief History of the Dynamic Web 189

Socket.lO 191

Basic Socket.lO Example 191

Sending Data from the Server to Clients 194

Broadcasting Data to Clients 199

Bi-Directional Data 204

Summary 209

Q&A 209

Workshop 210

Exercises 210

HOUR 13: A Socket.lO Chat Server 213

Express and Socket.lO 213

Adding Nicknames 216

Summary 235

Q&A 235

Workshop 236

Exercises 236

HOUR 14: A Streaming Twitter Client 237

Streaming APIs 237

Signing Up for Twitter 238

Using Twitter's API with Node.js 241

Extracting Meaning from the Data 244

Pushing Data to the Browser 247

Creating a Real-Time Lovehateometer 252

Summary 262

Q&A 263

Workshop 263

Exercises 264
x Sams Teach Yourself Node.js in 24 Hours

HOUR 15: JSON APIs 265

APIs 265

JSON 266

Sending JSON Data with Node.js 268

Creating JSON from JavaScript Objects 269

Consuming JSON Data with Node.js 271

Creating a JSON API with Node.js 275

Summary 285

Q&A 286

Workshop 286

Exercises 287

Part V: Exploring the Node.js API

HOUR 16: The Process Module 291

What Processes Are 291

Exiting and Errors in Processes 293

Processes and Signals 293

Sending Signals to Processes 295

Creating Scripts with Node.js 297

Passing Arguments to Scripts 298

Summary 301

Q&A 302

Workshop 302

Exercises 303

HOUR 17: The Child Process Module 305

What Is a Child Process? 305

Killing a Child Process 308

Communicating with a Child Process 309

The Cluster Module 311

Summary 314

Q&A 314

Workshop 314

Exercises 315
Contents xi

HOUR 18: The Events Module 317

Understanding Events 317

Demonstrating Events Through HTTP 321

Playing Ping-Pong with Events 324

Programming Event Listeners Dynamically 326

Summary 330

Q&A 330

Workshop 331

Exercises 331

HOUR 19: The Buffer Module 333

A Primer on Binary Data 333

Binary to Text 334

Binary and Nqde.js 335

What Are Buffers in Node.js? 338

Writing to Buffers 340

Appending to Buffers 340

Copying Buffers 342

Modifying Strings in Buffers 343

Summary 343

Q&A 343

Workshop 344

Exercises 344

HOUR 20: The Stream Module 345

A Primer on Streams 345

Readable Streams 347

Writable Streams 352

Piping Streams 353

Streaming MP3s 354

Summary 356

Q&A 356

Workshop 356

Exercises 357
xii Sams Teach Yourself Node.js in 24 Hours

Part VI: Further Node.js Development

361
HOUR 21: CoffeeScript
What Is 361
CoffeeScript?
and 363
Installing Running CoffeeScript
Use Pre-Compiler? 365
Why a

Features of 366
CoffeeScript
376
Debugging CoffeeScript
Reactions to CoffeeScript 377

378
Summary
Q&A 378

379
Workshop
Exercises 379

HOUR 22: Creating Node.js Modules 381

Create Modules? 381


Why
Modules 381
Popular Node.js
The File 383
package.json
Folder Structure 384

and Testing Your Module 385


Developing
Adding an Executable 388

Using Object-Oriented or Prototype-Based Programming 390

Code Via GitHub 391


Sharing
Using Travis CI 392

Publishing to npm 395

Publicizing Your Module 397

Summary 397

Q&A 397

Workshop 398

Exercises 398

HOUR 23: Creating Middleware with Connect 399

What Is Middleware? 399

Middleware in Connect 400

Access Control with Middleware 406


Contents xiii

Summary 414

Q&A 414

Workshop 415

Exercises 415

HOUR 24: Using Node.js with Backbone.js 417

What Is Backbone.js? 417

How Backbone.js Works 418

A Simple Backbone.js View 425

Creating Records with Backbone.js 429

Summary 432

Q&A 432

Workshop 433

Exercises 433

Index 435

You might also like