Node.js in Action

Node.js in Action

Paperback(New Edition)

$44.99 $49.99 Save 10% Current price is $44.99, Original price is $49.99. You Save 10%.
Choose Expedited Shipping at checkout for guaranteed delivery by Thursday, January 24

Product Details

ISBN-13: 9781617292576
Publisher: Manning Publications Company
Publication date: 08/30/2017
Edition description: New Edition
Pages: 392
Sales rank: 835,274
Product dimensions: 9.10(w) x 7.40(h) x 0.90(d)

About the Author

Alex R. Young, a writer and web developer, blogs regularly about JavaScript at DailyJS where you'll find several series on Node and client-side JavaScript. Alex speaks at JavaScript events around the UK.

Bradley Meck is a Node.js contributor, with lots of operational experience from load balancing to systems design. Has used Linux / BSD in production and dev ops.

Mike Cantelon is a web programmer with 10 years of experience in bespoke and product-oriented web application development

Tim Oxley is a JavaScript, React, WebVR & NodeJS developer and contributor. He is a founder of @campjsnews & SingaporeJS.

Marc Harter is a passionate JavaScript developer with deep experience in event-style programming. He works daily on large scale projects including high availability real-time applications, streaming interfaces, and other data intensive systems.

T.J. Holowaychuk is a prolific open-source engineer who has backed Node since its infancy. He has also authored many robust Node.js modules, including the popular Express web framework, Cluster, Stylus, and Jade, among many others.

Nathan Rajlich is an active Node developer who has been working with Node since its early days. He has authored an impressive collection of Node modules including NodObjC and maintains a port of Node that runs on Apple's iOS.

Table of Contents

Preface xv

Acknowledgments xvi

About this book xvii

About the author xix

About the cover illustration xx

Part 1 Welcome to Node 1

1 Welcome to Node.js 3

1.1 A typical Node web application 4

Nonblocking I/O 4

The event loop 5

1.2 ES2015, Node, and V8 6

Node and V8 9

Working with feature groups 10

Understanding Node's release schedule 10

1.3 Installing Node 10

1.4 Node's built-in tools 11

npm 12

The core modules 12

The debugger 14

1.5 The three main types of Node program 15

Web applications 15

Command-line tools and daemons 16

Desktop applications 17

Applications suited to Node 17

1.6 Summary 18

2 Node programming fundamentals 19

2.1 Organizing and reusing Node functionality 20

2.2 Starting a new Node project 22

Creating modules 22

2.3 Fine-tuning module creation by using module.exports 24

2.4 Reusing modules by using the node_modules folder 26

2.5 Exploring caveats 27

2.6 Using asynchronous programming techniques 28

2.7 Handling one-off events with callbacks 29

2.8 Handling repeating events with event emitters 33

An example event emitter 33

Responding to an event that should occur only once 34

Creating event emitters: a publish/subscribe example 34

Extending the event, emitter: a file watcher example 37

2.9 Challenges with asynchronous development 39

2.10 Sequencing asynchronous logic 40

2.11 When to use serial flow control 42

2.12 Implementing serial flow control 43

2.13 Implementing parallel flow control 45

2.14 Using community tools 48

2.15 Summary 49

3 What is a Node web application? 50

3.1 Understanding a Node web application's structure 51

Starting a new web app 52

Comparing other platforms 53

What's next? 53

3.2 Building a RESTful web service 54

3.3 Adding a database 57

Making your own model API 58

Making articles readable and saving them for later 60

3.4 Adding a user interface 61

Supporting multiple formats 61

Rendering templates 62

Using npm for client-side dependencies 63

3.5 Summary 64

Part 2 Web Development with Node 65

4 Front-end build systems 67

4.1 Understanding front-end development with Node 68

4.2 Using npm to run scripts 68

Creating custom npm scripts 69

Configuring front-end build tools 70

4.3 Providing automation with Gulp 71

Adding Gulp to a project 72

Creating and running Gulp tasks 72

Watching for changes 74

Using separate files for larger projects 74

4.4 Building web apps with webpack 75

Using bundles and plugins 76

Configuring and running webpack 76

Using webpack development server 77

Loading CommonJS modules and assets 79

4.5 Summary 80

5 Server-side frameworks 81

5.1 Personas 82

Phil: agency developer 82

Nadine: open source developer 82

Alice: product developer 83

5.2 What is a framework? 83

5.3 Koa 84

Setting up 86

Defining routes 86

REST APIs 87

Strengths 87

Weaknesses 87

5.4 Kraken 87

Setting up 88

Defining routes 88

REST APIs 89

Strengths 89

Weaknesses 90

5.5 Hapi 90

Setting up 90

Defining routes 91

Plugins 92

REST APIs 93

Strengths 93

Weaknesses 93

5.6 Sails.js 94

Setting up 94

Defining routes 95

REST APIs 95

Strengths 96

Weaknesses 96

5.7 DerbyJS 96

Setting up 96

Defining routes 97

REST APIs 98

Strengths 98

Weaknesses 98

5.8 Flatiron.js 99

Setting up 99

Defining routes 100

REST APIs 101

Strengths 101

Weaknesses 101

5.9 LoopBack 102

Setting up 102

Defining routes 104

REST APIs 104

Strengths 104

Weaknesses 105

5.10 Comparison 105

HTTP servers and routes 106

5.11 Writing modular code 106

5.12 Persona choices 106

5.13 Summary 107

6 Connect and Express in depth 108

6.1 Connect 109

Setting up a Connect application 109

Understanding how Connect middleware works 109

Combining middleware 110

Ordering middleware 111

Creating configurable middleware 112

Using error-handling middleware 114

6.2 Express 116

Generating the application skeleton 117

Configuring Express and your application 121

Rendering views 122

Express routing 101 128

Authenticating users 135

Registering new users 140

Logging in registered users 145

Working with user-loading middleware 148

Creating a public REST API 150

Enabling content negotiation 156

6.3 Summary 158

7 Web application templating 159

7.1 Using templating to keep code clean 159

Templating in action 161

Rendering HTML without a template 162

7.2 Templating with Embedded JavaScript 164

Creating a template 164

Integrating EJS into your application 166

Using EJS far client-side applications 167

7.3 Using the Mustache templating language with Hogan 168

Creating a template 168

Using Mustache tags 169

Fine-tuning Hogan 171

7.4 Templating with Pug 171

Pug basics 172

Logic in Pug templates 175

Organizing Pug templates 177

7.5 Summary 181

8 Storing application data 182

8.1 Relational databases 182

8.2 PostgreSQL 183

Performing installation and setup 183

Creating the database 183

Connecting to Postgres from Node 184

Defining tables 184

Inserting data 185

Updating data 185

Querying data 186

8.3 Knex 186

jQuery for databases 187

Connecting and running queries with Knex 187

Swopping the database back end 189

Beware of leaky abstractions 190

8.4 MySQL vs. PostgreSQL 190

8.5 ACID guarantees 191

Atomicity: transactions either succeed or fail in entirety 191

Consistency: constraints are always enforced 192

Isolation: concurrent transactions don't interfere 192

Durability: transactions are permanent 192

8.6 NoSQL 192

8.7 Distributed databases 193

8.8 MongoDB 194

Performing installation and setup 194

Connecting to MongoDB 195

Inserting documents 195

Querying 196

Using MongoDB identifiers 197

Using replica sets 199

Understanding write concerns 200

8.9 Key/value stores 201

8.10 Redis 202

Performing installation and setup 203

Performing initialization 203

Working with key/value pairs 204

Working with keys 204

Encoding and, data types 205

Using hashes 206

Using lists 207

Using sets 208

Providing pub/sub with channels 209

Improving Redis performance 210

8.11 Embedded databases 210

8.12 LevelDB 210

LevelUP and LevelDOWN 211

Installation 212

API overview 212

Initialization 212

Key/value encodings 212

Reading and writing key/value pairs 213

Pluggable bark ends 214

The modular database 215

8.13 Serialization and deserialization are expensive 216

8.14 In-browser storage 217

Web storage: localStorage and session-Storage 217

Reading and writing values 218

LocalForage 220

Reading and writing 221

8.15 Hosted storage 222

Simple Storage Service 222

8.16 Which database? 223

8.17 Summary 223

9 Testing Node applications 224

9.1 Unit testing 225

The assert module 226

Mocha 229

Vows 234

Chai 237

Should.js 238

Spies and stubs with Sinon.JS 240

9.2 Functional testing 242

Selenium 243

9.3 Dealing with failing tests 245

Getting more-detailed logs 246

Getting better stack traces 248

9.4 Summary 249

10 Deploying Node applications and maintaining uptime 250

10.1 Hosting Node applications 250

Platform as a service 251

Servers 253

Containers 254

10.2 Understanding deployment basics 255

Deploying from, a Git repository 255

Keeping Node running 256

10.3 Maximizing uptime and performance 257

Maintaining uptime with Upstart 258

The cluster-API: taking advantage of multiple cores 260

Hosting static files and proxying 262

10.4 Summary 263

Part 3 Beyond Web Develop 265

11 Writing command-line applications 267

11.1 Understanding conventions and philosophy 268

11.2 Introducing parse-json 269

11.3 Using command-line arguments 269

Parsing command-line arguments 269

Validating arguments 270

Passing stdin as a file 271

11.4 Sharing command-line tools with npm 272

11.5 Connecting scripts with pipes 272

Piping data into parse-json 273

Working with errors and exit codes 273

Using pipes in Node 275

Pipes and command execution order 276

11.6 Interpreting real-world scripts 277

11.7 Summary 278

12 Conquering the desktop with Electron 279

12.1 Introducing Electron 279

Electron's stack 280

Interface design 282

12.2 Creating an Electron app 282

12.3 Building a full desktop application 284

Bootstrapping React and Babel 285

Installing the dependencies 285

Setting up webpack 286

12.4 The React app 287

Defining the Request component 288

Defining the Response component 291

Communicating between React components 293

12.5 Builds and distribution 294

Building with Electron Packager 294

Packaging 295

12.6 Summary 296

Appendix A Installing Node 297

Appendix B Automating the web with scraping 301

Appendix C Connect's officially supported middleware 312

Glossary 347

Index 355

Customer Reviews

Most Helpful Customer Reviews

See All Customer Reviews