Preface
Node.js has been with me for many years now as a server-side development platform and also in the form of this book, which is now in its fourth edition in German and debuting with this first edition in English. Over time, the JavaScript world, and also Node.js itself, has changed a lot. Node.js has now grown up and is part of the mainstream. In contrast to the early days, when you had to convince funders, decision makers, and coworkers to use Node.js, it has become much easier now. In some cases, Node.js is already used as the base platform in projects. One aspect that has contributed decisively to this success story is the flexibility of the platform. You can use Node.js in small command-line tools, for building rapid prototypes, and also for large-scale server applications in an enterprise context. This book is intended to accompany you on your journey into the world of Node.js, to help you get started developing applications based on Node.js, and to serve as a reference in your daily work.
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).
Server-side JavaScript with Node.js differs in some aspects from developing client-side applications with HTML, CSS, and JavaScript, so you usually don't have direct visual feedback like you are used to in the browser. The architecture and design patterns you use on the server side also differ to some extent from those on the client side. Other patterns and paradigms are the same on both sides. Thus, Node.js is event-driven in many places, similar to the browser. This means that you have a lot to do with asynchronous operations because your application in Node.js usually runs in a single process, and performance bottlenecks can easily occur here if different operations block each other.
Node.js uses the same JavaScript engine as the Chrome browser: the V8 engine. Since the Node.js development team always keeps the engine up to date, you can develop modern JavaScript with Node.js and also have access to the latest language features. In addition to the engine, Node.js provides you with an extensive collection of core modules that you can use to solve your problems relatively close to the system level. However, you'll usually use external packages because, for most problems, there are already established solutions that you only need to adapt for your application. The module system of Node.js takes a prominent position here. A lot has happened in this area in recent years, and the platform has taken a big step forward in adapting the ECMAScript module system. Throughout this book, you'll learn more about the different layers of the module system and how you can use it to build your application.
As already mentioned, Node.js has been with me for a few years now in my development work. I started with web development under PHP, and like most web developers, I couldn't escape the influence of JavaScript. In addition to the aspect of implementing application logic in the frontend, I've also been primarily concerned with quality assurance in the area of JavaScript. The Node.js platform, along with the possibility to use JavaScript on the server as well, piqued my interest quite early. Like so many other developers, I was faced with the question: Can Node.js already be used productively in applications? I approached the answer to this question step by step in the past. Initially, the focus was on implementations of various sample applications in Node.js and testing out the toolset available for Node.js. Tooling requirements for Node.js range from the availability of a development environment to tools such as debuggers and platforms for continuous integration. In the first project, I used Node.js in combination with other programming languages. Node.js took care of the real-time communication in this case. After it had been shown over a longer period of time that Node.js can be used stably in such an environment, nothing stood in the way of its further use in other projects as well, so now I use Node.js on quite a few projects.
Nearly all examples in this book use the ECMAScript module system, which significantly modernizes the source code as a whole. Due to the new features of JavaScript, but also of Node.js, the source code has become more compact and also more readable than in the past. However, don't be put off by the new features and the multitude of packages. When working with JavaScript, it's important to learn about patterns and architectures so that you can evaluate new paradigms and packages and then deploy them with a manageable learning curve. Many of the patterns and best practices you're currently working with have been in place in a similar form since the earliest versions of Node.js. When working with Node.js, understanding the core of the platform is essential, and you acquire this best by experimenting with the platform. I invite you to follow the examples in the course of this book, to extend them, and to try out different tactics. In this book, you'll find both extensive connected examples, such as in Chapter 6 through Chapter 10, in which you implement a web application based on Express, and small, self-contained examples, such as in Chapter 17 on RxJS.
Structure of the Book
This book is roughly divided into four sections with thematically-related chapters.
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.
Node.js is primarily a platform for web development, which the second part of this book is devoted to covering. Here you'll learn how to implement a secure web server and also how to program extensive web applications with numerous components and modules. Numerous frameworks such as Express or libraries and template engines such as Pug offer assistance. The connection of various databases should also not be missing at this point. Here, with Node.js, you have a flexible and versatile platform as the basis for your application. The strength of a Node.js application is the combination of many small specialized individual parts. You'll learn different aspects of web development, including implementation with Node.js using practical examples and developing different types of web applications. You'll also learn about GraphQL, an alternative to the widely used REST interfaces. With Nest, you'll explore another framework for web backends that puts even more emphasis on structure and architecture than Express does.
JavaScript and asynchronicity go hand in hand. The third part of the book deals with different approaches to asynchronous programming. You'll learn both how to handle promises and child processes and how to use data streams in development. In this context, you won't be limited to your local system but can also communicate between different systems via TCP and UDP.
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.
Downloading the Code Samples
All code samples used in this book are available for download from the website at www.rheinwerk-computing.com/5556.
If you have any problems with the implementation or if I've overlooked an error despite careful checking, please feel free to contact me at node@sebastian-springer.com.
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...