did-you-know? rent-now

Amazon no longer offers textbook rentals. We do!

did-you-know? rent-now

Amazon no longer offers textbook rentals. We do!

We're the #1 textbook rental company. Let us show you why.

9781119962595

Smashing Node. JS : JavaScript Everywhere

by
  • ISBN13:

    9781119962595

  • ISBN10:

    1119962595

  • Edition: 2nd
  • Format: Paperback
  • Copyright: 2012-09-04
  • Publisher: Wiley
  • Purchase Benefits
  • Free Shipping Icon Free Shipping On Orders Over $35!
    Your order must be $35 or more to qualify for free economy shipping. Bulk sales, PO's, Marketplace items, eBooks and apparel do not qualify for this offer.
  • eCampus.com Logo Get Rewarded for Ordering Your Textbooks! Enroll Now
List Price: $44.99

Summary

True to the Smashing Magazine's promise, Smashing Node.js smashes readers with the information they need to understand what Node.js is and how they can put it to work developing more efficient and web apps. The book begins by addressing the biggest selling point of the technology: why you no longer need to learn more than one web development language, and how JavaScript can now solve all your web dev problems. Implicitly, this means employers and entrepreneurs no longer require expertise in several distinct languages and frameworks to create viable and successful web products. Smashing Node.js is example-heavy, explaining the practical advantages of Node.js that come from its specific design choices. Examples used in the book focus on apps that stand out because of their brevity, or benchmarks that show why Node.js is faster than most technologies readers are into already. The book also demonstrate how real-time technologies such as Socket.IO and HTML5 WebSockets can be implemented effortlessly in apps-another trait unique to Node.js. The book also uses an innovative online presentation, with sample chapters available online to encourage real-time online interaction among readers. The source code for the online sample chapters will be examined in detail within the book in chapters that aren't available to readers for free.

Author Biography

Guillermo Rauch is CTO and co-founder of San Francisco-based education startup LearnBoost. He is the inventor of several renowned Node.js projects, and he has been a speaker at JSConf as well as Nod.js workshops.

Table of Contents

PART I: GETTING STARTED: SETUP AND CONCEPTS 5

Chapter 1: The Setup 7

Installing on Windows 8

Installing on OS X 8

Installing on Linux 8

Compiling 9

Ensuring that it works 9

The Node REPL 9

Executing a file 10

NPM 10

Installing modules 11

Defining your own module 12

Installing binary utilities 13

Exploring the NPM registry 14

Summary 14

Chapter 2: JavaScript: An Overview 15

Introduction 15

Basic JavaScript 16

Types 16

Type hiccups 16

Functions 18

this, Function#call, and Function#apply 18

Functionarity 19

Closures 19

Classes 20

Inheritance 20

try {} catch {} 21

v8 JavaScript 22

Object#keys 22

Array#isArray 23

Array methods 23

String methods 24

JSON 24

Function#bind 24

Function#name 24

_proto_ (inheritance) 25

Accessors 25

Summary 26

Chapter 3: Blocking and Non-blocking IO 27

With great power comes great responsibility 28

Blocking-ness 29

A single-threaded world 31

Error handling 33

Stack traces 35

Summary 37

Chapter 4: Node JavaScript 39

The global object 40

Useful globals 40

The module system 41

Absolute and relative modules 41

Exposing APIs 44

Events 45

Buffers 47

Summary 48

PART II: ESSENTIAL NODE APIS 49

Chapter 5: CLI and FS APIs: Your First Application 51

Requirements 52

Writing your first program 52

Creating the module 53

Sync or async? 54

Understanding streams 55

Input and ouput 57

Refactoring 59

Interacting with the fs 61

Exploring the CLI 63

Argv 63

Working directory 64

Environmental variables 65

Exiting 65

Signals 65

ANSI escape codes 66

Exploring the fs module 66

Streams 67

Watch 67

Summary 68

Chapter 6: TCP 69

What are the characteristics of TCP? 70

Connection-oriented communication and same-order delivery 70

Byte orientation 70

Reliability 71

Flow control 71

Congestion control 71

Telnet 71

A TCP chat program 74

Creating the module 74

Understanding the net.server API 74

Receiving connections 76

Th e data event 77

State and keeping track of connections 79

Wrap up 81

An IRC Client program 83

Creating the module 83

Understanding the net#Stream API 84

Implementing part of the IRC protocol 84

Testing with a real-world IRC server 85

Summary 85

Chapter 7: HTTP 87

The structure of HTTP 88

Headers 89

Connections 93

A simple web server 94

Creating the module 95

Printing out the form 95

Methods and URLs 97

Data 99

Putting the pieces together 102

Bullet-proofing 103

A Twitter web client 104

Creating the module 104

Making a simple HTTP request 104

Sending a body of data 106

Getting tweets 107

A superagent to the rescue 110

Reloading HTTP servers with up 111

Summary 112

PART III: WEB DEVELOPMENT 113

Chapter 8: Connect 115

A simple website with HTTP 116

A simple website with Connect 119

Middleware 121

Writing reusable middleware 122

Static middleware 127

Query 128

Logger 129

Body parser 131

Cookies 134

Session 134

REDIS sessions 140

methodOverride 141

basicAuth 141

Summary 144

Chapter 9: Express 145

A simple express app 146

Creating the module 146

HTML 146

Setup 147

Defining routes 148

Search 150

Run 152

Settings 153

Template engines 154

Error handling 155

Convenience methods 155

Routes 157

Middleware 159

Organization strategies 160

Summary 162

Chapter 10: WebSocket 163

AJAX 164

HTML5 WebSocket 166

An Echo Example 167

Setting it up 167

Setting up the server 168

Setting up the client 169

Running the server 170

Mouse cursors 171

Setting up the example 171

Setting up the server 172

Setting up the client 174

Running the server 176

The Challenges Ahead 177

Close doesn’t mean disconnect 177

JSON 177

Reconnections 177

Broadcasting 177

WebSockets are HTML5: Older browsers don’t support them 177

The solution 178

Summary 178

Chapter 11: Socket.IO 179

Transports 180

Disconnected versus closed 180

Events 180

Namespaces 181

A chat program 182

Setting up the program 182

Setting up the server 182

Setting up the client 183

Events and Broadcasting 185

Ensuring reception 190

A DJ-by-turns application 191

Extending the chat 191

Integrating with the Grooveshark API 193

Playing 196

Summary 201

PART IV: DATABASES 203

Chapter 12: MongoDB 205

Installation 207

Accessing MongoDB: A user authentication example 208

Setting up the application 208

Creating the Express app 208

Connecting to MongoDB 212

Creating documents 214

Finding documents 215

Authentication middleware 217

Validation 218

Atomicity 219

Safe mode 219

Introducing Mongoose 220

Defining a model 220

Defining nested keys 222

Defining embedded documents 222

Setting up indexes 222

Middleware 223

Inspecting the state of the model 223

Querying 224

Extending queries 224

Sorting 224

Making Selections 224

Limiting 225

Skipping 225

Populating keys automatically 225

Casting 225

A mongoose example 226

Setting up the application 226

Refactoring 226

Setting up models 227

Summary 229

Chapter 13: MySQL 231

node-mysql 232

Setting it up 232

The Express app 232

Connecting to MySQL 234

Initializing the script 234

Creating data 238

Fetching data 242

sequelize 244

Setting up sequelize 245

Setting up the Express app 245

Connecting sequelize 248

Defining models and synchronizing 249

Creating data 250

Retrieving data 253

Removing data 254

Wrapping up 256

Summary 257

Chapter 14: Redis 259

Installing Redis 261

The Redis query language 261

Data types 262

Strings 263

Hashes 263

Lists 265

Sets 265

Sorted sets 266

Redis and Node 266

Implementing a social graph with node-redis 267

Summary 276

PART V: TESTING 277

Chapter 15: Code Sharing 279

What can be shared? 280

Writing compatible JavaScript 280

Exposing modules 280

Shimming ECMA APIs 282

Shimming Node APIs 283

Shimming browser APIs 284

Cross-browser inheritance 284

Putting it all together: browserbuild 285

A basic example 286

Summary 288

Chapter 16: Testing 289

Simple testing 290

The test subject 290

The test strategy 290

The test program 291

Expect.JS 292

API overview 292

Mocha 294

Testing asynchronous code 295

BDD style 297

TDD style 298

Exports style 298

Taking Mocha to the browser 299

Summary 300

Supplemental Materials

What is included with this book?

The New copy of this book will include any supplemental materials advertised. Please check the title of the book to determine if it should include any access cards, study guides, lab manuals, CDs, etc.

The Used, Rental and eBook copies of this book are not guaranteed to include any supplemental materials. Typically, only the book itself is included. This is true even if the title states it includes any access cards, study guides, lab manuals, CDs, etc.

Rewards Program