About this book

Before you get started, I’ll discuss Node.js and what you’ll learn in this book.

What is Node.js?

According to the Node.js website (https://nodejs.org/en/about/), Node.js is “an asynchronous event driven JavaScript runtime.” Let me break down that definition. Node.js reads, or interprets, your JavaScript code. You write code in JavaScript and then use your version of Node.js to run the code. How does that process work, exactly?

The Node.js runtime uses a JavaScript engine, a program that reads JavaScript code and executes its commands on the fly. Specifically, Node.js uses Google’s Chrome V8 Java-Script engine, an open-source interpreter that converts JavaScript to machine code—code that your computer can readily execute. This feature is useful because Google often updates and monitors its JavaScript engine for use in its Chrome web browser, where JavaScript engines traditionally run. Node.js adapts this engine to provide an environment for you to run JavaScript code that doesn’t require a web browser. Now, instead of reserving JavaScript for scripting on web pages, you can use it to build an entire application on the server (see unit 1).

Defining the terms asynchronous and event driven is important, as they’re fundamental elements of how JavaScript is used nowadays. Understanding their impact on Node.js applications is more important, however.

When a JavaScript application is launched, all the code in that application is loaded into memory. Every variable, function, and block of code is made available to the application, whether or not the code is executed right away. Why might certain code not run right away? Although defining and assigning a global variable may give that variable a value as soon as the application is launched, not all functions run unless they have a reason to do so. Some of these functions come in the form of event listeners—function objects that run a corresponding callback function when an event with a matching name is emitted. These functions sit around in memory until event emitters—objects that fire event names—trigger the event listeners to run their callback functions.

In this way, Node.js can run applications in a particularly fast, efficient manner. Whereas other platforms may need to recompile or run all of their code every time a request to run a certain command is made, Node.js loads JavaScript code only once, and it runs the functions and corresponding callback functions only when triggered to do so by events. JavaScript as a language supports event-driven development but doesn’t require it. Node.js takes advantage of this architecture by promoting the use of events as a way for the server to execute most of an application’s tasks, using the Node.js event-loop (see unit 1).

Last, why does it matter that Node.js is asynchronous? Well, JavaScript, by nature, is asynchronous, which means that tasks don’t necessarily run sequentially. If I want to call a function, log a comment, and change the background color of my web page, all these commands could potentially run instantaneously, but they won’t necessarily run in order. In fact, it’s likely that my comment will be logged before anything else happens.

The code in the listing that follows demonstrates this phenomenon. Although I call my callMe function first, change the background color of my web page to green next, and log a comment at the end, the order of events is reversed when I run this code in my web browser’s console.

Listing Example of asynchronous flow

Having an asynchronous runtime environment is great for web applications. Think about every time you’ve visited a website and the average time it took to load the page you requested. Suppose that you placed an order on Amazon.com and that while the order was processing (verifying your name, credit card information, shipping address, and other security measures), no other visitors to Amazon.com could load their web pages. This system would imply that the website used a single application process or thread (an operating-system resource dedicated to running a series of commands, handling every single task, and blocking other tasks from completion). Other web applications handle this scenario by creating new processes or threads, building bigger and more powerful machines to handle an influx of task requests.

Node.js requires only one executing thread (used by the event-loop), which can use other threads only when necessary for larger tasks. As a result, a Node.js application needs less processing power for creating and running tasks to completion because computer resources aren’t necessarily assigned and dedicated to each incoming task. In the Amazon example, Node.js might use its main thread to handle your request to process an order, send your information off to be verified, and continue to process other users’ requests to load web pages. When your order is processed, an event is emitted, triggering the main thread to let you know that your order was placed successfully. In other words, Node.js uses asynchrony to run parts of tasks and continue to other tasks before the first task completes. Instead of waiting for an operation from start to finish, Node.js registers event listeners, which are called when the task that was sent off is complete.

Ultimately, Node.js offers you a way to write JavaScript code without a web browser, and you can use this environment to design all types of applications. Most Node.js applications are web applications that use its asynchronous, event-driven nature to offer fast-loading, responsive web content.

In this book, you explore the architecture of a Node.js web application by evolving a basic JavaScript web server, using only the built-in Node.js tools, into a fully dynamic web application built with external open-source code libraries called Node.js packages (see unit 1).

Goals of the book

Node.js is only one of many platforms on which you can build an application. Because of its design, Node.js is particularly useful for building web applications—applications that handle requests over the internet and provide processed data and views in return. For many of you, the concept of building an application purely in JavaScript is both new and your ultimate goal. For others, this book is your introduction to web development. You’ve never built or fully understood the inner workings of a web application, and you’ve come here to learn how everything fits together.

Because the focus of this book is teaching web development through Node.js, I’m going to put a lot of focus on how a web application is architected, including initial setup, the ways dynamic pages are created, how a database is connected, and ways of preserving a user’s activity on your application. The goal is to clearly explain these concepts through examples and code that you can use and modify to create your own applications.

Who should read this book

This book is intended, first and foremost, for anyone who’s interested in learning about Node.js and the tools required to build a web application. If you have some familiarity with JavaScript but little experience with web development, this book is for you.

Because this book is project-based, readers need to be proficient in navigating their computers, typing, and working with a web browser. No experience in web-connected applications is expected. Readers with a background in backend or service technologies are good candidates for this book. New developers should have some familiarity with the following technologies:

  • JavaScript
  • HTML
  • CSS
  • Terminal/command line

Knowledge of JavaScript ES6 is beneficial but not required for this book.

How this book is organized: a road map

This book is divided into nine units. Each unit teaches a group of related concepts and builds on the preceding unit toward a more-complete, robust application. Unit 0 guides you through the Node.js installation and setup process, as well as the installation steps needed for other software used in this book. You continue from there to learn about some fundamental tools used in the Node.js core installation, including tools that come prepackaged with your installation of Node.js. In lesson 1, you start writing your first lines of JavaScript, which are run in the Node.js read-eval-print-loop (REPL), a window within your terminal window in which you can run JavaScript code. You end the unit by completing a few more exercises in the REPL environment and learning about Node.js modules.

Unit 1 jumps into building your first web server. The web server is the backbone of your web application, as it handles incoming requests for data to be processed and outgoing responses. Here, you learn how to initialize your Node.js application properly and load your first web page. Lessons 5 and 6 demonstrate how to use your web server to load images and other file types from your server. These lessons cover some of the fundamental concepts of interaction on the web. The unit concludes with your first capstone exercise: an opportunity to tie together the concepts you’ve learned by building your first web application.

The capstone exercise in unit 1 carries over into unit 2, where you learn about Express.js, a web framework. In this unit, you learn how web frameworks help speed up the development process by implementing much of the code you wrote in unit 1. Lessons 9 and 10 cover how to use Express.js to architect a standard web application, and lesson 11 teaches you how to handle errors that occur on your server. Unit 2 concludes with your second capstone exercise, in which you re-create your web application by using the Express.js web framework.

Unit 3 shows you how to save application data through the minimum database theory needed to connect a database and start persisting your application’s data. In this unit, you learn about MongoDB, a leading database used in Node.js applications. You start by getting familiar with the MongoDB environment, creating database collections and documents. Then you connect your database to your application with the help of a Node.js package called Mongoose. Lessons 14 and 15 teach you how to organize your data in Mongoose models as one part of the model-view-controller (MVC) architecture taught in this book. The unit ends with an opportunity to add a database and models to your capstone project.

Unit 4 builds on the concept of models by discussing the standard functionality expected of your models. In this unit, you learn about create, read, update, and delete (CRUD) functions and see why they’re helpful to have for the major models in your application. At this point, you develop the ability to create and modify data in your application from the web browser. This unit also helps you complete some of the code needed in your application controllers and shows you how to link web forms to your application’s server and models. The unit concludes with a capstone exercise in which you build the CRUD functions for your user model.

Unit 5 introduces user authentication and the code you need to allow unique users to sign up for and log in to your application. In lesson 22, you add sessions and cookies to your application to allow information to be shared between the application server and your users’ computers. This technique helps preserve a user’s state while they navigate your application. Next, you learn how to encrypt your user passwords. This lesson guides you through the standard security practices expected in protecting your application data. Last, you set up an authentication system to analyze and approve user data and then apply these techniques to your capstone project. By the end of this unit, you’ll have an application in which you can selectively display content to logged-in users.

Unit 6 focuses on an often-under-taught element of application development: application programming interfaces (APIs). In lesson 26, you’re introduced to some ways that you can extend your application to serve data in other ways beyond a web page. These alternative data avenues enable your application to connect with external services that might use your application’s data. You might later build a mobile app or Amazon Alexa skill that needs to use your application’s data but can’t read a normal web page’s contents, for example. A useful API can deliver that data in multiple data formats. In lessons 27 and 28, you build out your application’s API and use it within the application by creating a pop-out window with a list of data accessed through an API endpoint. At the end of the unit, you secure your API by creating an API token system and applying the same techniques to your capstone project.

When the core of your application is complete, you move to unit 7, where you learn about building a live-chat feature in your application. In Node.js, you use Socket.io, a library that connects to your application’s normal web server and enhances it to allow open streams of communication among users. The lessons in this unit break down the steps you need to take to set up Socket.io and (later) to associate messages with users and save those associations in your database. By the end of the unit, you have a fully functioning chat system running in your capstone project.

In unit 8, you configure your application to get deployed online. Up to this point, you’ve viewed your work on your own machine, with no opportunity for external users to sign up for and use your application. In lesson 34, you save your application code to Git and upload the first live version of your application to Heroku. In this lesson, you’re provided a URL with which you can share your application with family members, friends, and co-workers. Lessons 35 and 36 introduce some ways to clean your application code and monitor your application as it begins its journey on the internet. In the final lesson, I introduced some ways in which you can test your code. Testing is an important element in the development process; it may ensure that your code continues to function as expected as you make changes and add features.

About the code

This book contains many examples of source code, both in numbered listings and inline with normal text. In both cases, source code is formatted in a fixed-width font like this to separate it from ordinary text. Sometimes, code is also in bold to highlight code that it has changed from previous steps in the chapter, such as when a new feature adds to an existing line of code.

In many cases, the original source code has been reformatted; I’ve added line breaks and reworked indentation to accommodate the available page space in the book. In rare cases, even reformatting wasn’t enough, so some listings include line-continuation markers (). Additionally, comments in the source code have often been removed from the listings when the code is described in the text. Code annotations accompany many of the listings, highlighting important concepts.

All code examples in this book are available for download from the Manning website at https://www.manning.com/books/get-programming-with-node-js and from GitHub at https://github.com/JonathanWexler/get-programming-with-nodejs. The code examples are organized by lesson and unit. Within each lesson’s folder, you’ll find a start folder, containing the code you can use and build on from the beginning of that lesson, and a finish folder, which contains the final working code for that lesson. Any future updates to this book’s code will be added to lesson-specific folders titled updated.

Software requirements

For this book, you need a computer with at least 500 MB of RAM and 500 MB persistent memory. Most modern computers come with plenty of space and the specifications needed to run a Node.js application.

Node.js supports 32-bit and 64-bit Windows and Linux installations and the standard 64-bit Mac OS installation, as specified at https://nodejs.org/en/download/.

You also need a text editor to write your code. I recommend installing the Atom text editor, available for free at https://atom.io.

You need a web browser to test your web application. I recommend installing the Google Chrome browser, which is available for free at https://www.google.com/chrome

You also need to install the Heroku command-line interface and Git on your machine (instructions listed in unit 0).

liveBook discussion forum

Purchase of Get Programming with Node.js includes free access to a private web forum run by Manning Publications, where you can make comments about the book, ask technical questions, and receive help from the author and from other users. To access the forum, go to https://livebook.manning.com/#!/book/get-programming-with-node-js/discussion. You can also learn more about Manning’s forums and the rules of conduct at https://livebook.manning.com/#!/discussion.

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the author can take place. It isn’t a commitment to any specific amount of participation on the part of the author, whose contribution to the forum remains voluntary (and unpaid). We suggest that you try asking the author some challenging questions lest his interest stray! The forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset