ebook img

Node.js in Action PDF

394 Pages·2017·11.19 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Node.js in Action

SECOND EDITION Alex Young Bradley Meck Mike Cantelon Tim Oxley WITH Marc Harter T.J. Holowaychuk Nathan Rajlich M A N N I N G Praise for the First Edition From the first edition of Node.js in Action by Mike Cantelon, Marc Harter, T.J. Holowaychuk, and Nathan Rajlich. “The content ramps up nicely from basic to advanced.” From the Foreword by Isaac Z. Schlueter, Node.js Project Lead “The definitive guide to Node and the Node.js ecosystem.” Kevin Baister, 1KB Software Solutions “Superbly written with practical (and even funny) real-world examples.” Àlex Madurell, Polymedia SpA “Thoroughly enjoyable...will get you up and running very quickly.” Gary Ewan Park, Honeywell “An excellent resource written by the people behind the code.” Brian Falk, NodeLingo, GoChime Licensed to Samir Mashlum <[email protected]> ii PRAISE FOR THE FIRST EDITION Licensed to Samir Mashlum <[email protected]> Node.js in Action SECOND EDITION ALEX YOUNG BRADLEY MECK MIKE CANTELON WITH TIM OXLEY MARC HARTER T.J. HOLOWAYCHUK NATHAN RAJLICH MANNING SHELTER ISLAND Licensed to Samir Mashlum <[email protected]> For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: [email protected] ©2017 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editor: Cynthia Kane 20 Baldwin Road Review editor: Aleksandar Dragosavljevic´ PO Box 761 Technical development editor: Stan Bice Shelter Island, NY 11964 Project editors: Kevin Sullivan, David Novak Copyeditor: Sharon Wilkey Proofreader: Melody Dolab Technical proofreader: Doug Warren Typesetter and cover design: Marija Tudor ISBN 9781617292576 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – EBM – 22 21 20 19 18 17 Licensed to Samir Mashlum <[email protected]> brief contents PART 1 WELCOME TO NODE ....................................................... 1 1 ■ Welcome to Node.js 3 2 ■ Node programming fundamentals 19 3 ■ What is a Node web application? 50 PART 2 WEB DEVELOPMENT WITH NODE ................................... 65 4 ■ Front-end build systems 67 5 ■ Server-side frameworks 81 6 ■ Connect and Express in depth 108 7 ■ Web application templating 159 8 ■ Storing application data 182 9 ■ Testing Node applications 224 10 ■ Deploying Node applications and maintaining uptime 250 PART 3 BEYOND WEB DEVELOPMENT ....................................... 265 11 ■ Writing command-line applications 267 12 ■ Conquering the desktop with Electron 279 v Licensed to Samir Mashlum <[email protected]> vi BRIEF CONTENTS Licensed to Samir Mashlum <[email protected]> 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 vii Licensed to Samir Mashlum <[email protected]> viii CONTENTS 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 Licensed to Samir Mashlum <[email protected]> CONTENTS ix 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 Licensed to Samir Mashlum <[email protected]>

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.