Nodejs The Comprehensive Guide To Serverside Javascript Programming Springer pdf download
Nodejs The Comprehensive Guide To Serverside Javascript Programming Springer pdf download
https://ebookbell.com/product/nodejs-the-comprehensive-guide-to-
serverside-javascript-programming-springer-56848688
https://ebookbell.com/product/nodejs-the-comprehensive-guide-to-
serverside-javascript-programming-comprehensive-sebastian-
springer-46231558
https://ebookbell.com/product/nodejs-the-comprehensive-guide-springer-
sebastian-50742464
https://ebookbell.com/product/the-node-beginner-book-a-comprehensive-
nodejs-tutorial-manuel-kiessling-4104638
https://ebookbell.com/product/mastering-nodejs-web-development-go-on-
a-comprehensive-journey-from-the-fundamentals-to-advanced-web-
development-with-nodejs-adam-freeman-58369470
Mastering Nodejs Web Development Go On A Comprehensive Journey From
The Fundamentals To Advanced Web Development With Nodejs 1st Edition
Adam Freeman
https://ebookbell.com/product/mastering-nodejs-web-development-go-on-
a-comprehensive-journey-from-the-fundamentals-to-advanced-web-
development-with-nodejs-1st-edition-adam-freeman-200671740
Nodejs The Right Way Practical Serverside Javascript That Scales 1st
Edition Jim R Wilson
https://ebookbell.com/product/nodejs-the-right-way-practical-
serverside-javascript-that-scales-1st-edition-jim-r-wilson-4946042
Nodejs The Collection James Hibbard Camilo Reyes Michael Wanyoike Mark
Brown Manjunath M Jay Raj Lukas White James Kolce Simon Holmes Paul
Orac Patrick Catanzariti Jani Hartikainen M David Green Craig Buckler
Olayinka Omole Ahmed Bouchefra Paul Sauve
https://ebookbell.com/product/nodejs-the-collection-james-hibbard-
camilo-reyes-michael-wanyoike-mark-brown-manjunath-m-jay-raj-lukas-
white-james-kolce-simon-holmes-paul-orac-patrick-catanzariti-jani-
hartikainen-m-david-green-craig-buckler-olayinka-omole-ahmed-
bouchefra-paul-sauve-11295854
https://ebookbell.com/product/fullstack-nodejs-the-complete-guide-to-
building-production-apps-with-nodejs-revision-2p-20191125-davit-
guttman-nate-murray-ed-11798290
https://ebookbell.com/product/fullstack-nodejs-the-complete-guide-to-
building-production-apps-with-nodejs-davit-guttman-nate-murray-
ed-11380816
Sebastian Springer
Node.js
The Comprehensive Guide
Imprint
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.
Megan Fuerst
Editor, SAP PRESS
meganf@rheinwerk-publishing.com
www.rheinwerk-computing.com
Rheinwerk Publishing • Boston, MA
Notes on Usage
For detailed and legally binding usage conditions, please refer to the
section Legal Notes.
You are reading this e-book in a file format (EPUB or Mobi) that
makes the book content adaptable to the display options of your
reading device and to your personal needs. That’s a great thing; but
unfortunately not every device displays the content in the same way
and the rendering of features such as pictures and tables or
hyphenation can lead to difficulties. This e-book was optimized for
the presentation on as many common reading devices as possible.
Dear Reader
Notes on Usage
Table of Contents
Preface
1 Basic Principles
2 Installation
4 Node.js Modules
5 HTTP
6 Express
6.1 Structure
6.2 Installation
6.3 Basic Principles
6.3.1 Request
6.3.2 Response
6.4 Setup
6.4.1 Structure of an Application
6.5 Movie Database
6.5.1 Routing
6.5.2 Controller
6.5.3 Model
6.5.4 View
6.6 Middleware
6.6.1 Custom Middleware
6.6.2 Morgan: Logging Middleware for Express
6.6.3 Delivering Static Content
6.7 Extended Routing: Deleting Data Records
6.8 Creating and Editing Data Records: Body
Parser
6.8.1 Handling Form Input: Body Parser
6.9 Express 5
6.10 HTTPS and HTTP/2
6.10.1 HTTPS
6.10.2 HTTP/2
6.11 Summary
7 Template Engines
8 Connecting Databases
8.1 Node.js and Relational Databases
8.1.1 MySQL
8.1.2 SQLite
8.1.3 Object-Relational Mapping
8.2 Node.js and Nonrelational Databases
8.2.1 Redis
8.2.2 MongoDB
8.3 Summary
9.1 Passport
9.2 Setup and Configuration
9.2.1 Installation
9.2.2 Configuration
9.2.3 Strategy Configuration
9.3 Logging In to the Application
9.3.1 Login Form
9.3.2 Securing Resources
9.3.3 Logging Out
9.3.4 Connecting to the Database
9.4 Accessing Resources
9.4.1 Access Restriction
9.4.2 Submitting Ratings
9.5 Summary
10 REST Server
11 GraphQL
13 Type-Safe Applications in
Node.js
16 Asynchronous Programming
17 RxJS
17.1 Basic Principles
17.1.1 Installation and Integration
17.1.2 Observable
17.1.3 Observer
17.1.4 Operator
17.1.5 Example of RxJS in Node.js
17.2 Operators
17.2.1 Creation Operators
17.2.2 Transformation Operators
17.2.3 Filtering Operators
17.2.4 Join Operators
17.2.5 Error Handling Operators
17.2.6 Utility Operators
17.2.7 Conditional Operators
17.2.8 Connection Operators
17.2.9 Conversion Operators
17.3 Subjects
17.4 Schedulers
17.5 Summary
18 Streams
18.1 Introduction
18.1.1 What Is a Stream?
18.1.2 Stream Usages
18.1.3 Available Streams
18.1.4 Stream Versions in Node.js
18.1.5 Streams Are EventEmitters
18.2 Readable Streams
18.2.1 Creating a Readable Stream
18.2.2 Readable Stream Interface
18.2.3 Events of a Readable Stream
18.2.4 Error Handling in Readable Streams
18.2.5 Methods
18.2.6 Piping
18.2.7 Readable Stream Modes
18.2.8 Switching to Flowing Mode
18.2.9 Switching to the Paused Mode
18.2.10 Custom Readable Streams
18.2.11 Example of a Readable Stream
18.2.12 Readable Shortcut
18.3 Writable Streams
18.3.1 Creating a Writable Stream
18.3.2 Events
18.3.3 Error Handling in Writable Streams
18.3.4 Methods
18.3.5 Buffering Write Operations
18.3.6 Flow Control
18.3.7 Custom Writable Streams
18.3.8 Writable Shortcut
18.4 Duplex Streams
18.4.1 Duplex Streams in Use
18.4.2 Custom Duplex Streams
18.4.3 Duplex Shortcut
18.5 Transform Streams
18.5.1 Custom Transform Streams
18.5.2 Transform Shortcut
18.6 Gulp
18.6.1 Installation
18.6.2 Example of a Build Process with Gulp
18.7 Summary
20 Socket Server
20.1 Unix Sockets
20.1.1 Accessing the Socket
20.1.2 Bidirectional Communication
20.2 Windows Pipes
20.3 TCP Sockets
20.3.1 Data Transfer
20.3.2 File Transfer
20.3.3 Flow Control
20.3.4 Duplex
20.3.5 Pipe
20.4 UDP Sockets
20.4.1 Basic Principles of a UDP Server
20.4.2 Example Illustrating the UDP Server
20.5 Summary
21 Package Manager
22 Quality Assurance
23 Testing
24 Security
25.1 Deployment
25.1.1 Simple Deployment
25.1.2 File Synchronization via rsync
25.1.3 Application as a Service
25.1.4 node_modules in Deployment
25.1.5 Installing Applications Using Node Package
Manager
25.1.6 Installing Packages Locally
25.2 Tool Support
25.2.1 Grunt
25.2.2 Gulp
25.2.3 Node Package Manager
25.3 Scaling
25.3.1 Child Processes
25.3.2 Load Balancer
25.3.3 Node in the Cloud
25.4 pm2: Process Management
25.5 Docker
25.5.1 Dockerfile
25.5.2 Starting the Container
25.6 Summary
26 Performance
28 Deno
28.1 The Ten Things Ryan Dahl Regrets about
Node.js
28.1.1 Promises
28.1.2 Security
28.1.3 The Generate Your Projects Build System
28.1.4 Package.json
28.1.5 Node_modules
28.1.6 Optional File Extension When Loading Modules
28.1.7 Index.js
28.1.8 What’s Going on Now with Node.js
28.2 Installing Deno
28.2.1 Deno Command-Line Interface
28.3 Execution
28.3.1 Running a TypeScript Application
28.4 Handling Files
28.4.1 The Task: Copying a File
28.4.2 Processing Command-Line Options
28.4.3 Reading Files
28.4.4 Permissions in Deno
28.4.5 readTextFile Function
28.4.6 Writing Files with Deno
28.5 Web Server with Deno
28.6 Module System
28.6.1 Loading External Modules into Deno
28.6.2 deno.land/x
28.6.3 Using Node Package Manager Packages
28.7 Summary
The Author
Index
Service Pages
Legal Notes
Preface
In this book, you'll learn the basics of Node.js, learn more about how
the platform is built, and work with the various interfaces that Node.js
makes available to you.
Note that to work with this book, you should have a solid basic
knowledge of JavaScript. While I do cover some language features
throughout this book, such as destructuring, promises, and the
module system, the focus is on Node.js, and so you should take a
look at the Mozilla Developer Network
(https://developer.mozilla.org/de/), which comprehensively explains
all aspects of JavaScript. I would also like to take this opportunity to
recommend Philip Ackermann’s JavaScript: The Comprehensive
Guide (SAP PRESS, 2022, www.rheinwerk-computing.com/5554).
The first part of the book covers the basics of Node.js and the
general structuring of applications based on Node.js. Here we take a
look at the development history of the platform and the installation.
You’ll also receive a practical introduction to the module system.
The last part of the book deals with problems beyond pure
programming and is intended to give you important tips for dealing
with Node.js in everyday life as a developer. This concerns topics
that take place directly in the course of development, such as
dealing with Node Package Manager (npm) as a package manager
and handling quality assurance of applications by implementing
tests, and also code analysis and debugging. Another very important
topic is application security. In a separate chapter, you’ll learn more
about attack possibilities and how you can counter them to protect
yourself and the users of your application. The following chapters are
dedicated to the deployment and scalability of Node.js applications,
in which you’ll learn more about the performance of applications as
well as the use of Node.js in a microservice architecture. The last
chapter introduces you to Deno, the biggest competitor with Node.js.
In that chapter, you’ll also learn why it’s still worth your while to get
involved with Node.js, despite the competition.
Acknowledgments
Finally, I would like to thank all the people involved in this book,
especially Philip Ackermann, who contributed many valuable
comments and tips.
I would also like to thank Sibylle Feldmann for proofreading and fine-
tuning the language of my book.
A big thank you also goes to the entire team at Rheinwerk Verlag,
especially Patricia Schiewald.
Sebastian Springer
Aßling, Germany
1 Basic Principles
Bringing more dynamics into web pages was the original idea behind
JavaScript. The scripting language was intended to compensate for
the weaknesses of HTML when it came to responding to user input.
The history of JavaScript dates back to 1995 when it was developed
under the code name Mocha by Brendan Eich, a developer at
Netscape. One of the most remarkable facts about JavaScript is that
the first prototype of this successful and globally used language was
developed in just 10 days. Still in the year of its creation, Mocha was
renamed to LiveScript and finally to JavaScript in a cooperation
between Netscape and Sun. This was mainly for marketing
purposes, as at that time it was assumed that Java would become
the leading language in client-side web development.
In 1997, the first draft of the language standard was created at Ecma
International. The entire language core of the script language is
recorded under the cryptic designation ECMA-262 or ISO/IEC
16262. The current standard can be found at www.ecma-
international.org/publications/standards/Ecma-262.htm. Due to this
standardization, vendor-independent JavaScript is also referred to as
ECMAScript. Until a few years ago, the ECMAScript standard was
versioned in integers starting at 1. Since version 6, the versions are
also provided with year numbers. ECMAScript in version 8 is
therefore referred to as ECMAScript 2017. As a rule, you can
assume that the manufacturers support the older versions of the
standard quite well. You must either enable newer features by
configuration flags in the browser or simulate them via polyfills (that
is, recreating the features in JavaScript). A good overview of the
currently supported features is provided by kangax’s compatibility
table, which can be found at http://kangax.github.io/compat-
table/es6/. A version adapted for Node.js can be reached at
http://node.green/.
1.1.1 Origins
Node.js was originally developed by Ryan Dahl, a PhD student in
mathematics who thought better of it, abandoned his efforts, and
instead preferred to travel to South America with a one-way ticket
and very little money in his pocket. There, he kept his head above
water by teaching English. During this time, he got in touch with PHP
as well as Ruby and discovered his affection for web development.
The problem with working with the Ruby framework, called Rails,
was that it couldn’t deal with concurrent requests without any
workaround. The applications were too slow and utilized the CPU
entirely. Dahl found a solution to his problems with Mongrel, a web
server for applications based on Ruby.
The task that actually led to the creation of Node.js is quite trivial
from today’s point of view. In 2005, Dahl was looking for an elegant
way to implement a progress bar for file uploads. However, the
technologies available at the time only allowed unsatisfactory
solutions. Regarding file transfers, HTTP was used for relatively
small files, and File Transfer Protocol (FTP) was used for larger files.
The status of the upload was queried using long polling, which is a
technique where the client sends long-lived requests to the server,
and the server uses the open channel for replies. Dahl’s first attempt
to implement a progress bar took place in Mongrel. After sending the
file to the server, it checked the status of the upload using a large
number of Asynchronous JavaScript and XML (AJAX) requests and
displayed it graphically in a progress bar. However, the downside of
this implementation was Ruby’s single-threaded approach and the
large number of requests that were required.
ebookbell.com