Sams Teach Yourself Node.js in 24 Hours

Series
Sams
Author
George Ornbo  
Publisher
Sams Publishing
Cover
Softcover
Edition
1
Language
English
Total pages
464
Pub.-date
September 2012
ISBN13
9780672335952
ISBN
0672335956
Related Titles


Product detail

Product Price CHF Available  
9780672335952
Sams Teach Yourself Node.js in 24 Hours
45.10 not defined

Description

In just 24 sessions of one hour or less, Sams Teach Yourself Node.js in 24 Hours will help you master the Node.js platform and use it to build server-side applications with extraordinary speed and scalability. Using this text’s straightforward, step-by-step approach, you’ll move from basic installation, configuration, and programming all the way through real-time messaging between browser and server, testing and deployment. Every lesson and case-study application builds on what you’ve already learned, giving you a rock-solid foundation for real-world success!

  • Step-by-step instructions carefully walk you through the most common Node.js development tasks.
  • Quizzes and Exercises at the end of each chapter help you test your knowledge.
  • By the Way notes present valuable additional information related to the discussion.
  • Did You Know? tips offer advice or show you easier ways to perform tasks.
  • Watch Out! cautions alert you to possible problems and give you advice on how to avoid them.
Learn how to
  • Create end-to-end applications entirely in JavaScript
  • Master essential Node.js concepts like callbacks and quickly create your first program
  • Create basic sites with the HTTP module and Express web framework
  • Manage data persistence with Node.js and MongoDB
  • Debug and test Node.js applications
  • Deploy Node.js applications to thirdparty services, such as Heroku and Nodester
  • Build powerful real-time solutions, from chat servers to Twitter clients
  • Create JSON APIs using JavaScript on the server
  • Use core components of the Node.js API, including processes, child processes, events, buffers, and streams

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

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

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

Workshop     132

Exercises     132

Part III: 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

Part IV: Intermediate Sites with Node.js

Hour 12: Introducting Socket.IO      189

Now for Something Completely Different     189

Brief History of the Dynamic Web     189

Socket.IO     191

Basic Socket.IO 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.IO Chat Server     213

Express and Socket.IO     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

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

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 Node.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

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

Part VI: Further Node.js Development

Hour 21: CoffeeScript      361

What Is CoffeeScript?      361

Installing and Running CoffeeScript     363

Why Use a Pre-Compiler?     365

Features of CoffeeScript     366

Debugging CoffeeScript     376

Reactions to CoffeeScript     377

Summary     378

Q&A     378

Workshop     379

Exercises     379

Hour 22: Creating Node.js Modules      381

Why Create Modules?      381

Popular Node.js Modules     381

The package.json File     383

Folder Structure     384

Developing and Testing Your Module     385

Adding an Executable     388

Using Object-Oriented or Prototype-Based Programming     390

Sharing Code Via GitHub     391

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

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

Author

George Ornbo is a web developer at pebble {code}, an agency that specializes in delivering web applications built with Node.js, Ruby, .NET, and F#. Ornbo has more than eight years of experience delivering web applications for both startups and established corporate clients. He is currently working on an online gaming startup.