Node.js Design Patterns
Node.js Design Patterns
js Design Patterns
Third Edition
Mario Casciaro
Luciano Mammino
BIRMINGHAM - MUMBAI
Node.js Design Patterns
Third Edition
Copyright © 2020 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy
of the information presented. However, the information contained in this book is
sold without warranty, either express or implied. Neither the authors, nor Packt
Publishing or its dealers and distributors, will be held liable for any damages
caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
Producer: Tushar Gupta
Acquisition Editor – Peer Reviews: Suresh Jain
Project Editor: Tom Jacob
Content Development Editors: Joanne Lovell, Bhavesh Amin
Copy Editor: Safis Editing
Technical Editor: Saby D'silva
Proofreader: Safis Editing
Indexer: Manju Arasan
Presentation Designer: Sandip Tadge
ISBN 978-1-83921-411-0
www.packt.com
Table of Contents
Prefacexi
Chapter 1: The Node.js Platform 1
The Node.js philosophy 2
Small core 2
Small modules 2
Small surface area 3
Simplicity and pragmatism 4
How Node.js works 5
I/O is slow 5
Blocking I/O 5
Non-blocking I/O 6
Event demultiplexing 7
The reactor pattern 9
Libuv, the I/O engine of Node.js 11
The recipe for Node.js 12
JavaScript in Node.js 13
Run the latest JavaScript with confidence 13
The module system 14
Full access to operating system services 14
Running native code 15
Summary16
Chapter 2: The Module System 17
The need for modules 18
Module systems in JavaScript and Node.js 19
The module system and its patterns 20
The revealing module pattern 20
[i]
Table of Contents
CommonJS modules 22
A homemade module loader 22
Defining a module 24
module.exports versus exports 25
The require function is synchronous 26
The resolving algorithm 26
The module cache 28
Circular dependencies 29
Module definition patterns 33
Named exports 33
Exporting a function 34
Exporting a class 35
Exporting an instance 36
Modifying other modules or the global scope 37
ESM: ECMAScript modules 38
Using ESM in Node.js 39
Named exports and imports 39
Default exports and imports 42
Mixed exports 43
Module identifiers 45
Async imports 45
Module loading in depth 48
Loading phases 48
Read-only live bindings 49
Circular dependency resolution 50
Modifying other modules 56
ESM and CommonJS differences and interoperability 60
ESM runs in strict mode 60
Missing references in ESM 60
Interoperability61
Summary62
Chapter 3: Callbacks and Events 63
The Callback pattern 64
The continuation-passing style 64
Synchronous CPS 65
Asynchronous CPS 65
Non-CPS callbacks 67
Synchronous or asynchronous? 67
An unpredictable function 68
Unleashing Zalgo 68
Using synchronous APIs 70
Guaranteeing asynchronicity with deferred execution 72
[ ii ]
Table of Contents
[ iii ]
Table of Contents
[ iv ]
Table of Contents
[v]
Table of Contents
[ vi ]
Table of Contents
[ viii ]
Table of Contents
[ ix ]
Table of Contents
[x]