Node.js The Comprehensive Guide to Server-Side JavaScript Programming (Sebastian Springer) (Z-Library)

Author: Sebastian Springer

技术

If you’re developing server-side JavaScript applications, you need Node.js! Start with the basics of the Node.js environment: installation, application structure, and modules. Then follow detailed code examples to learn about web development using frameworks like Express and Nest.js. Learn about different approaches to asynchronous programming, including RxJS and data streams. Details on peripheral topics such as testing, security, performance, and more, make this your all-in-one daily reference for Node.js! Your complete guide to backend programming with JavaScript Install the Node.js environment and learn to use core frameworks Debug, scale, test, and optimize your applications Get practical code examples as downloadable product supplements Getting Started with Node.js Begin your journey with Node.js. Learn about the core components of the environment such as the V8 engine and libraries. Then install Node.js and explore application development tools and the module system. Developing Applications Develop web applications by following practical code examples. Set up a web server using HTTP and develop apps step by step using the Express and Nest.js frameworks. Connect databases, generate interfaces using the REST server and GraphQL, implement command-line tools, handle asynchronous programming, and more. Managing Applications Manage your Node.js applications from development to deployment. Learn how to use package managers, implement tests, and protect against security threats. Get expert tips on scalability and performance to optimize your apps.

📄 File Format: PDF
💾 File Size: 19.5 MB
76
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
(This page has no text content)
📄 Page 2
Rheinwerk Computing The Rheinwerk Computing series from Rheinwerk Publishing offers new and established professionals comprehensive guidance to enrich their skillsets and enhance their career prospects. Our publications are written by leading experts in the fields of programming, administration, security, analytics, and more. Each book is detailed and hands-on to help readers develop essential, practical skills that they can apply to their daily work. For further information, please visit our website: www.rheinwerk-computing.com. Philip Ackermann JavaScript: The Comprehensive Guide 2022, approx. 1292 pp, paperback and e-book www.rheinwerk-computing.com/5554 Christian Ullenboom Java: The Comprehensive Guide 2022, approx.1258 pp, paperback and e-book www.rheinwerk-computing.com/5557 Johannes Ernesti, Peter Kaiser Python 3: The Comprehensive Guide 2022, approx. 1078 pp, paperback and e-book www.rheinwerk-computing.com/5566 Bernd Öggl, Michael Kofler Git: Project Management for Developers and DevOps Teams 2023, approx. 415 pp, paperback and e-book www.rheinwerk-computing.com/5555
📄 Page 3
Sebastian Springer Node.js The Comprehensive Guide
📄 Page 4
Dear Reader, Fun fact about us: Rheinwerk Publishing has branches in both Bonn, Germany, and Boston, Massachusetts. The benefit of this international setup is that we can publish both German-language books and English-language books—the challenge is translating them. When our colleagues at Rheinwerk Verlag in Bonn publish a book that we would also like to provide to English-speaking readers, or vice versa, the manuscript undergoes the translation process. This means that either the author, or a third party, must translate every sentence, diagram, and piece of code. However, receiving the translated manu- script several months later isn’t the end of the story. Both the author and the editor must review and fine-tune the manuscript: the author ensures no errors have been introduced, and the editor realigns any variations in our formatting standards. Then, the copyeditor edits each chapter line-by-line to polish the grammar and style in the book’s new language. Node.js: The Comprehensive Guide is one of these translations, brought to you from its fourth German edition. Ironically, the topic of this book, Node.js, is the solution for this “translation” process in the programming world. Node.js allows your client- and server-side scripts to be written in a single language: JavaScript. In these pages, you’ll find the instructions and practical examples you need to make the most of the Node.js environment for unified web application development. What did you think about Node.js: The Comprehensive Guide? Your comments and sug- gestions are the most useful tools to help us make our books the best they can be. Please feel free to contact me and share any praise or criticism you may have. Thank you for purchasing a book from Rheinwerk Publishing! Megan Fuerst Editor, SAP PRESS meganf@rheinwerk-publishing.com www.rheinwerk-computing.com Rheinwerk Publishing • Boston, MA
📄 Page 5
Imprint This e-book is a publication many contributed to, specifically: Editor Megan Fuerst Acquisitions Editor Hareem Shafi German Edition Editor Patricia Schiewald Translation Winema Language Services, Inc. Copyeditor Julie McNamee Cover Design Graham Geary Photo Credit Shutterstock: 1940821471/© banjongseal956 Layout Design Vera Brauner Production E-Book Graham Geary Typesetting E-Book III-satz, Germany We hope that you liked this e-book. Please share your feedback with us and read the Service Pages to find out how to contact us. Library of Congress Cataloging-in-Publication Control Number: 2022023980 ISBN 978-1-4932-2292-6 (print) ISBN 978-1-4932-2293-3 (e-book) ISBN 978-1-4932-2294-0 (print and e-book) © 2022 by Rheinwerk Publishing, Inc., Boston (MA) 1st edition 2022 4th German edition published 2021 by Rheinwerk Verlag, Bonn, Germany
📄 Page 6
Contents Foreword .................................................................................................................................................. 25 Preface ....................................................................................................................................................... 27 1 Basic Principles 31 1.1 The Story of Node.js ............................................................................................................. 33 1.1.1 Origins ........................................................................................................................ 33 1.1.2 Birth of Node.js ........................................................................................................ 34 1.1.3 Breakthrough of Node.js ...................................................................................... 34 1.1.4 Node.js Conquers Windows ................................................................................ 35 1.1.5 io.js: The Fork of Node.js ...................................................................................... 36 1.1.6 Node.js Reunited ..................................................................................................... 36 1.1.7 Deno: A New Star in the JavaScript Sky .......................................................... 36 1.1.8 OpenJS Foundation ................................................................................................ 37 1.2 Organization of Node.js ..................................................................................................... 37 1.2.1 Technical Steering Committee .......................................................................... 37 1.2.2 Collaborators ........................................................................................................... 37 1.2.3 Community Committee ....................................................................................... 38 1.2.4 Work Groups ............................................................................................................ 38 1.2.5 OpenJS Foundation ................................................................................................ 38 1.3 Versioning of Node.js .......................................................................................................... 38 1.3.1 Long-Term Support Releases .............................................................................. 39 1.4 Benefits of Node.js ................................................................................................................ 40 1.5 Areas of Use for Node.js ..................................................................................................... 40 1.6 The Core: V8 Engine ............................................................................................................. 41 1.6.1 Memory Model ........................................................................................................ 42 1.6.2 Accessing Properties .............................................................................................. 43 1.6.3 Machine Code Generation .................................................................................. 45 1.6.4 Garbage Collection ................................................................................................ 46 1.7 Libraries around the Engine .............................................................................................. 47 1.7.1 Event Loop ................................................................................................................. 48 1.7.2 Input and Output ................................................................................................... 50 1.7.3 libuv ............................................................................................................................ 50 1.7.4 Domain Name System .......................................................................................... 51 1.7.5 Crypto ......................................................................................................................... 52 1.7.6 Zlib ............................................................................................................................... 527
📄 Page 7
Contents1.7.7 HTTP Parser ............................................................................................................... 52 1.8 Summary ................................................................................................................................... 53 2 Installation 55 2.1 Installing Packages ............................................................................................................... 56 2.1.1 Linux ........................................................................................................................... 57 2.1.2 Windows ................................................................................................................... 60 2.1.3 macOS ........................................................................................................................ 63 2.2 Compiling and Installing .................................................................................................... 68 2.3 Node Version Manager ....................................................................................................... 71 2.4 Node and Docker ................................................................................................................... 71 2.5 Summary ................................................................................................................................... 72 3 Developing Your First Application 73 3.1 Interactive Mode .................................................................................................................... 73 3.1.1 General Use .............................................................................................................. 74 3.1.2 Other REPL Commands ......................................................................................... 75 3.1.3 Saving and Loading in the REPL ......................................................................... 76 3.1.4 Context of the REPL ............................................................................................... 77 3.1.5 REPL History .............................................................................................................. 77 3.1.6 REPL Mode ................................................................................................................. 78 3.1.7 Searching in the REPL ............................................................................................ 78 3.1.8 Asynchronous Operations in the REPL ............................................................ 79 3.2 The First Application ............................................................................................................ 79 3.2.1 Web Server in Node.js ........................................................................................... 80 3.2.2 Extending the Web Server ................................................................................... 83 3.2.3 Creating an HTML Response ............................................................................... 85 3.2.4 Generating Dynamic Responses ........................................................................ 86 3.3 Debugging Node.js Applications .................................................................................... 88 3.3.1 Navigating in the Debugger ............................................................................... 90 3.3.2 Information in the Debugger ............................................................................. 91 3.3.3 Breakpoints ............................................................................................................... 93 3.3.4 Debugging with Chrome Developer Tools ..................................................... 96 3.3.5 Debugging in the Development Environment ............................................. 978
📄 Page 8
Contents3.4 nodemon Development Tool ........................................................................................... 98 3.5 Summary ................................................................................................................................... 99 4 Node.js Modules 101 4.1 Modular Structure ................................................................................................................. 101 4.2 Core Modules ........................................................................................................................... 103 4.2.1 Stability ...................................................................................................................... 104 4.2.2 List of Core Modules .............................................................................................. 105 4.2.3 Loading Core Modules .......................................................................................... 108 4.2.4 Global Objects ......................................................................................................... 111 4.3 JavaScript Module Systems ............................................................................................... 121 4.3.1 CommonJS ................................................................................................................ 121 4.3.2 ECMAScript Modules ............................................................................................. 122 4.4 Creating and Using Your Own Modules ...................................................................... 124 4.4.1 Modules in Node.js: CommonJS ........................................................................ 125 4.4.2 Custom Node.js Modules ..................................................................................... 126 4.4.3 Modules in Node.js: ECMAScript ....................................................................... 127 4.4.4 Exporting Different Types of Data .................................................................... 129 4.4.5 The modules Module ............................................................................................ 130 4.4.6 Module Loader ......................................................................................................... 131 4.5 Summary ................................................................................................................................... 135 5 HTTP 137 5.1 Web Server ............................................................................................................................... 137 5.1.1 Server Object ............................................................................................................ 137 5.1.2 Server Events ............................................................................................................ 142 5.1.3 Request Object ........................................................................................................ 145 5.1.4 Handling the Request Body (Update) .............................................................. 152 5.1.5 Delivering Static Content .................................................................................... 157 5.1.6 File Upload ................................................................................................................ 159 5.1.7 Fine-Tuning the Frontend .................................................................................... 163 5.2 Node.js as HTTP Client ......................................................................................................... 164 5.2.1 Requests with the http Module ......................................................................... 164 5.2.2 The request Package .............................................................................................. 165 5.2.3 HTML Parser ............................................................................................................. 1679
📄 Page 9
Contents5.3 Secure Communication with HTTPS .............................................................................. 168 5.3.1 Creating Certificates .............................................................................................. 168 5.3.2 Using HTTPS in the Web Server ......................................................................... 169 5.4 HTTP/2 ........................................................................................................................................ 170 5.4.1 HTTP/2 Server .......................................................................................................... 170 5.4.2 HTTP/2 Client ........................................................................................................... 173 5.5 Summary ................................................................................................................................... 175 6 Express 177 6.1 Structure .................................................................................................................................... 177 6.2 Installation ............................................................................................................................... 178 6.3 Basic Principles ....................................................................................................................... 179 6.3.1 Request ...................................................................................................................... 180 6.3.2 Response ................................................................................................................... 180 6.4 Setup ........................................................................................................................................... 181 6.4.1 Structure of an Application ................................................................................. 182 6.5 Movie Database ..................................................................................................................... 185 6.5.1 Routing ....................................................................................................................... 186 6.5.2 Controller .................................................................................................................. 189 6.5.3 Model .......................................................................................................................... 190 6.5.4 View ............................................................................................................................ 192 6.6 Middleware .............................................................................................................................. 193 6.6.1 Custom Middleware .............................................................................................. 194 6.6.2 Morgan: Logging Middleware for Express ..................................................... 195 6.6.3 Delivering Static Content .................................................................................... 197 6.7 Extended Routing: Deleting Data Records ................................................................. 199 6.8 Creating and Editing Data Records: Body Parser ..................................................... 201 6.8.1 Handling Form Input: Body Parser ................................................................... 205 6.9 Express 5 .................................................................................................................................... 208 6.10 HTTPS and HTTP/2 ................................................................................................................ 209 6.10.1 HTTPS .......................................................................................................................... 209 6.10.2 HTTP/2 ....................................................................................................................... 210 6.11 Summary ................................................................................................................................... 21210
📄 Page 10
Contents7 Template Engines 213 7.1 Custom Template Engine ................................................................................................... 214 7.2 Template Engines in Practice: Pug ................................................................................. 215 7.2.1 Installation ................................................................................................................ 215 7.2.2 Pug and Express: Integration ............................................................................. 216 7.2.3 Variables in Pug ...................................................................................................... 219 7.2.4 Specific Features of Pug ....................................................................................... 221 7.2.5 Conditions and Loops ............................................................................................ 222 7.2.6 Extends and Includes ............................................................................................ 223 7.2.7 Mixins ......................................................................................................................... 226 7.2.8 Using Pug without Express ................................................................................. 228 7.2.9 Compiling .................................................................................................................. 228 7.3 Handlebars ............................................................................................................................... 229 7.3.1 Installation ................................................................................................................ 230 7.3.2 Integration with Express ...................................................................................... 230 7.3.3 Conditions and Loops ............................................................................................ 232 7.3.4 Partials ....................................................................................................................... 234 7.3.5 Custom Helpers ....................................................................................................... 236 7.3.6 Handlebars without Express .............................................................................. 238 7.4 Summary ................................................................................................................................... 239 8 Connecting Databases 241 8.1 Node.js and Relational Databases ................................................................................. 242 8.1.1 MySQL ........................................................................................................................ 242 8.1.2 SQLite .......................................................................................................................... 251 8.1.3 Object-Relational Mapping ................................................................................. 257 8.2 Node.js and Nonrelational Databases ......................................................................... 260 8.2.1 Redis ............................................................................................................................ 260 8.2.2 MongoDB .................................................................................................................. 265 8.3 Summary ................................................................................................................................... 27211
📄 Page 11
Contents9 Authentication and Session Handling 273 9.1 Passport ..................................................................................................................................... 273 9.2 Setup and Configuration .................................................................................................... 274 9.2.1 Installation ................................................................................................................ 274 9.2.2 Configuration .......................................................................................................... 274 9.2.3 Strategy Configuration ......................................................................................... 275 9.3 Logging In to the Application ........................................................................................... 277 9.3.1 Login Form ................................................................................................................ 277 9.3.2 Securing Resources ................................................................................................ 280 9.3.3 Logging Out .............................................................................................................. 281 9.3.4 Connecting to the Database ............................................................................... 282 9.4 Accessing Resources ............................................................................................................. 285 9.4.1 Access Restriction ................................................................................................... 285 9.4.2 Submitting Ratings ................................................................................................ 289 9.5 Summary ................................................................................................................................... 294 10 REST Server 295 10.1 Introduction to REST and Usage in Web Applications .......................................... 295 10.2 Accessing the Application .................................................................................................. 296 10.2.1 Postman ..................................................................................................................... 296 10.2.2 cURL ............................................................................................................................. 297 10.3 Adaptations to the Application Structure .................................................................. 297 10.4 Read Requests ......................................................................................................................... 298 10.4.1 Reading All Data Records of a Resource .......................................................... 298 10.4.2 Accessing a Data Record ...................................................................................... 301 10.4.3 Error Handling ......................................................................................................... 302 10.4.4 Sorting the List ........................................................................................................ 304 10.4.5 Controlling the Output Format ......................................................................... 307 10.5 Write Requests ....................................................................................................................... 309 10.5.1 POST: Creating New Data Records ................................................................... 309 10.5.2 PUT: Modifying Existing Data Records ............................................................ 312 10.5.3 DELETE: Deleting Data Records .......................................................................... 314 10.6 Authentication via JWTs .................................................................................................... 316 10.6.1 Login ........................................................................................................................... 317 10.6.2 Safeguarding Resources ....................................................................................... 31912
📄 Page 12
Contents10.6.3 Accessing User Information in the Token ...................................................... 321 10.7 OpenAPI Specification: Documentation with Swagger ....................................... 324 10.8 Validation .................................................................................................................................. 329 10.8.1 Installation and First Validation ........................................................................ 330 10.8.2 Checking Requests with a Validation Schema ............................................. 332 10.9 Summary ................................................................................................................................... 335 11 GraphQL 337 11.1 GraphQL Libraries .................................................................................................................. 338 11.2 Integration with Express .................................................................................................... 339 11.3 GraphiQL .................................................................................................................................... 341 11.4 Reading Data via the Interface ........................................................................................ 342 11.4.1 Parameterizing Queries ........................................................................................ 345 11.5 Write Accesses to the GraphQL Interface ................................................................... 347 11.5.1 Creating New Data Records ................................................................................ 347 11.5.2 Updating and Deleting Data Records .............................................................. 350 11.6 Authentication for the GraphQL Interface ................................................................. 353 11.7 Summary ................................................................................................................................... 355 12 Real-Time Web Applications 357 12.1 The Sample Application ...................................................................................................... 358 12.2 Setup ........................................................................................................................................... 358 12.3 WebSockets .............................................................................................................................. 364 12.3.1 The Server Side ........................................................................................................ 366 12.3.2 The Client Side ......................................................................................................... 367 12.3.3 User List ..................................................................................................................... 370 12.3.4 Logout ........................................................................................................................ 373 12.4 Socket.IO ................................................................................................................................... 377 12.4.1 Installation and Integration ................................................................................ 378 12.4.2 Socket.IO API ............................................................................................................ 379 12.5 Summary ................................................................................................................................... 38313
📄 Page 13
Contents13 Type-Safe Applications in Node.js 385 13.1 Type Systems for Node.js ................................................................................................... 386 13.1.1 Flow ............................................................................................................................. 386 13.1.2 TypeScript ................................................................................................................. 390 13.2 Tools and Configuration ..................................................................................................... 392 13.2.1 Configuring the TypeScript Compiler .............................................................. 393 13.2.2 Integration into the Development Environment ......................................... 394 13.2.3 ESLint .......................................................................................................................... 395 13.2.4 ts-node ....................................................................................................................... 396 13.3 Basic Principles ....................................................................................................................... 398 13.3.1 Data Types ................................................................................................................ 398 13.3.2 Functions ................................................................................................................... 400 13.3.3 Modules ..................................................................................................................... 402 13.4 Classes ........................................................................................................................................ 403 13.4.1 Methods .................................................................................................................... 404 13.4.2 Access Modifiers ..................................................................................................... 405 13.4.3 Inheritance ................................................................................................................ 405 13.5 Interfaces ................................................................................................................................... 406 13.6 Type Aliases in TypeScript ................................................................................................. 408 13.7 Generics ..................................................................................................................................... 409 13.8 TypeScript in Use in a Node.js Application ................................................................. 410 13.8.1 Type Definitions ...................................................................................................... 410 13.8.2 Creating Custom Type Definitions ................................................................... 410 13.8.3 Sample Express Application ................................................................................ 411 13.9 Summary ................................................................................................................................... 412 14 Web Applications with Nest 413 14.1 Installation and Getting Started with Nest ............................................................... 414 14.2 Nest Command-Line Interface ......................................................................................... 416 14.2.1 Commands for Operating and Running the Application .......................... 416 14.2.2 Creating Structures in the Application ........................................................... 418 14.3 Structure of the Application ............................................................................................. 419 14.3.1 Root Directory with the Configuration Files ................................................. 419 14.3.2 src Directory: Core of the Application ............................................................. 420 14.3.3 Other Directories of the Application ................................................................ 42014
📄 Page 14
Contents14.4 Modules: Logical Units in the Source Code ................................................................ 421 14.4.1 Creating Modules ................................................................................................... 422 14.4.2 Module Decorator .................................................................................................. 423 14.5 Controllers: Endpoints of an Application .................................................................... 423 14.5.1 Creating a Controller ............................................................................................. 424 14.5.2 Implementing a Controller .................................................................................. 424 14.5.3 Integrating and Checking the Controller ........................................................ 426 14.6 Providers: Business Logic of the Application ............................................................ 428 14.6.1 Creating and Including a Service ....................................................................... 428 14.6.2 Implementing the Service ................................................................................... 429 14.6.3 Integrating the Service via Nest’s Dependency Injection ......................... 431 14.7 Accessing Databases ............................................................................................................ 432 14.7.1 Setup and Installation .......................................................................................... 432 14.7.2 Accessing the Database ....................................................................................... 435 14.8 Documenting the Endpoints with OpenAPI .............................................................. 439 14.9 Authentication ........................................................................................................................ 442 14.9.1 Setup ........................................................................................................................... 442 14.9.2 Authentication Service ......................................................................................... 443 14.9.3 Login Controller: Endpoint for User Login ...................................................... 445 14.9.4 Protecting Routes ................................................................................................... 446 14.10 Outlook: Testing in Nest .................................................................................................... 449 14.11 Summary ................................................................................................................................... 451 15 Node on the Command Line 453 15.1 Basic Principles ....................................................................................................................... 453 15.1.1 Structure .................................................................................................................... 454 15.1.2 Executability ............................................................................................................. 455 15.2 Structure of a Command-Line Application ................................................................. 456 15.2.1 File and Directory Structure ................................................................................ 456 15.2.2 Package Definition ................................................................................................. 456 15.2.3 Math Trainer Application ..................................................................................... 457 15.3 Accessing Input and Output ............................................................................................. 461 15.3.1 Output ........................................................................................................................ 461 15.3.2 Input ........................................................................................................................... 462 15.3.3 User Interaction with the readline Module ................................................... 463 15.3.4 Options and Arguments ....................................................................................... 46715
📄 Page 15
Contents15.4 Tools ............................................................................................................................................ 469 15.4.1 Commander ............................................................................................................. 469 15.4.2 Chalk ........................................................................................................................... 471 15.4.3 node-emoji ............................................................................................................... 473 15.5 Signals ........................................................................................................................................ 476 15.6 Exit Codes .................................................................................................................................. 478 15.7 Summary ................................................................................................................................... 479 16 Asynchronous Programming 481 16.1 Basic Principles of Asynchronous Programming ..................................................... 481 16.1.1 The child_process Module ................................................................................... 485 16.2 Running External Commands Asynchronously ........................................................ 486 16.2.1 The exec Method .................................................................................................... 487 16.2.2 The spawn Method ................................................................................................ 489 16.3 Creating Node.js Child Processes with fork Method ............................................. 492 16.4 The cluster Module ............................................................................................................... 496 16.4.1 Main Process ............................................................................................................ 497 16.4.2 Worker Processes .................................................................................................... 501 16.5 Worker Threads ...................................................................................................................... 504 16.5.1 Shared Memory in the worker_threads Module ......................................... 505 16.6 Promises in Node.js .............................................................................................................. 507 16.6.1 Using util.promisify to Use Promises Where None Actually Exist ......... 510 16.6.2 Concatenating Promises ...................................................................................... 511 16.6.3 Multiple Parallel Operations with Promise.all .............................................. 512 16.6.4 Fastest Asynchronous Operation with Promise.race ................................. 513 16.6.5 Overview of the Promise Functions ................................................................. 514 16.7 Async Functions ...................................................................................................................... 514 16.7.1 Top-Level Await ....................................................................................................... 516 16.8 Summary ................................................................................................................................... 517 17 RxJS 519 17.1 Basic Principles ....................................................................................................................... 520 17.1.1 Installation and Integration ................................................................................ 52116
📄 Page 16
Contents17.1.2 Observable ................................................................................................................ 521 17.1.3 Observer .................................................................................................................... 522 17.1.4 Operator .................................................................................................................... 523 17.1.5 Example of RxJS in Node.js .................................................................................. 523 17.2 Operators .................................................................................................................................. 525 17.2.1 Creation Operators ................................................................................................ 527 17.2.2 Transformation Operators .................................................................................. 529 17.2.3 Filtering Operators ................................................................................................. 532 17.2.4 Join Operators .......................................................................................................... 534 17.2.5 Error Handling Operators .................................................................................... 535 17.2.6 Utility Operators ..................................................................................................... 537 17.2.7 Conditional Operators .......................................................................................... 538 17.2.8 Connection Operators ........................................................................................... 539 17.2.9 Conversion Operators ........................................................................................... 540 17.3 Subjects ...................................................................................................................................... 540 17.4 Schedulers ................................................................................................................................. 542 17.5 Summary ................................................................................................................................... 543 18 Streams 545 18.1 Introduction ............................................................................................................................. 545 18.1.1 What Is a Stream? .................................................................................................. 545 18.1.2 Stream Usages ......................................................................................................... 546 18.1.3 Available Streams ................................................................................................... 546 18.1.4 Stream Versions in Node.js ................................................................................. 547 18.1.5 Streams Are EventEmitters ................................................................................. 548 18.2 Readable Streams .................................................................................................................. 548 18.2.1 Creating a Readable Stream ............................................................................... 548 18.2.2 Readable Stream Interface .................................................................................. 550 18.2.3 Events of a Readable Stream .............................................................................. 550 18.2.4 Error Handling in Readable Streams ................................................................ 552 18.2.5 Methods .................................................................................................................... 553 18.2.6 Piping .......................................................................................................................... 553 18.2.7 Readable Stream Modes ...................................................................................... 554 18.2.8 Switching to Flowing Mode ................................................................................ 554 18.2.9 Switching to the Paused Mode .......................................................................... 555 18.2.10 Custom Readable Streams .................................................................................. 555 18.2.11 Example of a Readable Stream .......................................................................... 556 18.2.12 Readable Shortcut .................................................................................................. 55817
📄 Page 17
Contents18.3 Writable Streams ................................................................................................................... 559 18.3.1 Creating a Writable Stream ................................................................................ 560 18.3.2 Events ......................................................................................................................... 560 18.3.3 Error Handling in Writable Streams ................................................................. 562 18.3.4 Methods .................................................................................................................... 562 18.3.5 Buffering Write Operations ................................................................................ 563 18.3.6 Flow Control ............................................................................................................. 564 18.3.7 Custom Writable Streams ................................................................................... 565 18.3.8 Writable Shortcut ................................................................................................... 566 18.4 Duplex Streams ...................................................................................................................... 566 18.4.1 Duplex Streams in Use ......................................................................................... 566 18.4.2 Custom Duplex Streams ...................................................................................... 567 18.4.3 Duplex Shortcut ...................................................................................................... 567 18.5 Transform Streams ............................................................................................................... 568 18.5.1 Custom Transform Streams ................................................................................ 568 18.5.2 Transform Shortcut ............................................................................................... 569 18.6 Gulp ............................................................................................................................................. 570 18.6.1 Installation ................................................................................................................ 570 18.6.2 Example of a Build Process with Gulp ............................................................. 571 18.7 Summary ................................................................................................................................... 572 19 Working with Files 573 19.1 Synchronous and Asynchronous Functions ............................................................... 573 19.2 Existence of Files .................................................................................................................... 575 19.3 Reading Files ............................................................................................................................ 576 19.3.1 Promise-Based API ................................................................................................. 581 19.4 Error Handling ......................................................................................................................... 582 19.5 Writing to Files ....................................................................................................................... 582 19.6 Directory Operations ............................................................................................................ 586 19.7 Advanced Operations .......................................................................................................... 589 19.7.1 The watch Method ................................................................................................. 591 19.7.2 Access Permissions ................................................................................................ 592 19.8 Summary ................................................................................................................................... 59418
📄 Page 18
Contents20 Socket Server 595 20.1 Unix Sockets ............................................................................................................................ 596 20.1.1 Accessing the Socket ............................................................................................. 598 20.1.2 Bidirectional Communication ............................................................................ 600 20.2 Windows Pipes ....................................................................................................................... 602 20.3 TCP Sockets ............................................................................................................................... 603 20.3.1 Data Transfer ........................................................................................................... 605 20.3.2 File Transfer .............................................................................................................. 606 20.3.3 Flow Control ............................................................................................................. 607 20.3.4 Duplex ........................................................................................................................ 609 20.3.5 Pipe .............................................................................................................................. 609 20.4 UDP Sockets ............................................................................................................................. 610 20.4.1 Basic Principles of a UDP Server ........................................................................ 611 20.4.2 Example Illustrating the UDP Server ............................................................... 612 20.5 Summary ................................................................................................................................... 614 21 Package Manager 615 21.1 Most Common Operations ................................................................................................ 616 21.1.1 Searching Packages ............................................................................................... 616 21.1.2 Installing Packages ................................................................................................. 617 21.1.3 Viewing Installed Packages ................................................................................. 622 21.1.4 Using Packages ........................................................................................................ 623 21.1.5 Updating Packages ................................................................................................ 624 21.1.6 Removing Packages ............................................................................................... 625 21.1.7 Overview of the Most Important Commands .............................................. 626 21.2 Advanced Operations .......................................................................................................... 627 21.2.1 Structure of a Module ........................................................................................... 627 21.2.2 Creating Custom Packages .................................................................................. 630 21.2.3 Node Package Manager Scripts ......................................................................... 632 21.3 Tools for Node Package Manager .................................................................................. 634 21.3.1 Node License Finder ............................................................................................... 634 21.3.2 Verdaccio ................................................................................................................... 635 21.3.3 npm-check-updates ............................................................................................... 636 21.3.4 npx ............................................................................................................................... 637 21.4 Yarn .............................................................................................................................................. 637 21.5 Summary ................................................................................................................................... 63819
📄 Page 19
Contents22 Quality Assurance 641 22.1 Style Guides ............................................................................................................................. 642 22.1.1 Airbnb Style Guide ................................................................................................. 642 22.2 Linter ........................................................................................................................................... 643 22.2.1 ESLint .......................................................................................................................... 644 22.3 Prettier ....................................................................................................................................... 648 22.3.1 Installation ................................................................................................................ 649 22.3.2 Execution ................................................................................................................... 649 22.4 Programming Mistake Detector: Copy/Paste Detector ....................................... 649 22.4.1 Installation ................................................................................................................ 650 22.4.2 Execution ................................................................................................................... 651 22.5 Husky .......................................................................................................................................... 652 22.6 Summary ................................................................................................................................... 653 23 Testing 655 23.1 Unit Testing .............................................................................................................................. 655 23.1.1 Directory Structure ................................................................................................ 656 23.1.2 Unit Tests and Node.js .......................................................................................... 656 23.1.3 Arrange, Act, Assert ............................................................................................... 657 23.2 Assertion Testing ................................................................................................................... 658 23.2.1 Exceptions ................................................................................................................. 661 23.2.2 Testing Promises ..................................................................................................... 662 23.3 Jasmine ....................................................................................................................................... 663 23.3.1 Installation ................................................................................................................ 664 23.3.2 Configuration .......................................................................................................... 664 23.3.3 Tests in Jasmine ...................................................................................................... 665 23.3.4 Assertions .................................................................................................................. 667 23.3.5 Spies ............................................................................................................................ 670 23.3.6 beforeEach and afterEach ................................................................................... 671 23.4 Jest ............................................................................................................................................... 671 23.4.1 Installation ................................................................................................................ 671 23.4.2 First Test .................................................................................................................... 672 23.5 Practical Example of Unit Tests with Jest ................................................................... 674 23.5.1 The Test ...................................................................................................................... 675 23.5.2 Implementation ...................................................................................................... 67620
📄 Page 20
Contents23.5.3 Triangulation: Second Test ................................................................................. 677 23.5.4 Optimizing the Implementation ....................................................................... 678 23.6 Dealing with Dependencies: Mocking ......................................................................... 679 23.7 Summary ................................................................................................................................... 681 24 Security 683 24.1 Filter Input and Escape Output ........................................................................................ 684 24.1.1 Filter Input ................................................................................................................ 684 24.1.2 Blacklisting and Whitelisting ............................................................................. 684 24.1.3 Escape Output ......................................................................................................... 685 24.2 Protecting the Server ........................................................................................................... 686 24.2.1 User Permissions .................................................................................................... 686 24.2.2 Problems Caused by the Single-Threaded Approach ................................. 688 24.2.3 Denial-of-Service Attacks ..................................................................................... 690 24.2.4 Regular Expressions ............................................................................................... 692 24.2.5 HTTP Header ............................................................................................................ 693 24.2.6 Error Messages ........................................................................................................ 695 24.2.7 SQL Injections .......................................................................................................... 695 24.2.8 eval .............................................................................................................................. 699 24.2.9 Method Invocation ................................................................................................. 700 24.2.10 Overwriting Built-Ins ............................................................................................. 702 24.3 Node Package Manager Security .................................................................................... 704 24.3.1 Permissions .............................................................................................................. 704 24.3.2 Node Security Platform ........................................................................................ 705 24.3.3 Quality Aspect ......................................................................................................... 705 24.3.4 Node Package Manager Scripts ......................................................................... 706 24.4 Client Protection .................................................................................................................... 707 24.4.1 Cross-Site Scripting ................................................................................................ 707 24.4.2 Cross-Site Request Forgery .................................................................................. 709 24.5 Summary ................................................................................................................................... 711 25 Scalability and Deployment 713 25.1 Deployment ............................................................................................................................. 713 25.1.1 Simple Deployment ............................................................................................... 713 25.1.2 File Synchronization via rsync ............................................................................ 71521
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List