Lesson 0. Setting up Node.js and the JavaScript engine

In this lesson, you get an overview of what you’ll be learning in this book and why it’s important. Whether you’re new to web development or a seasoned developer looking to build better applications, this lesson acts as a starting guide for entering the world of Node.js.

This lesson covers

  • Reviewing what you’re going to learn
  • Understanding Node.js
  • Learning why we develop in Node.js
  • Preparing yourself for this book

0.1. What you’re going to learn

The goal of this book is to teach you how to build web applications on a platform called Node.js, using the JavaScript language. Starting with this lesson, each unit aims to build on the concepts and development skills of the last.

As you work through each lesson, you pick up new web development concepts, terminology, and coding skills that will help you build a web application. Although the book revolves around using Node.js, many of the concepts taught in the following units apply to other leading platforms and programming languages.

Note

Web development skills are different from typical software engineering or computer theory knowledge. In addition to teaching coding concepts, this book helps explain how the internet works beyond your project. I’ll explain as much as I can to make things easier.

Following is an overview of what you’ll learn in each unit:

  • Unit 0 gives you the background knowledge you need to get started and walks you through the installation of Node.js and development tools.
  • Unit 1 covers some basic web development concepts and provides guiding steps toward building your first web application in Node.js from scratch.
  • Unit 2 introduces you to Express.js, a web framework that most Node.js developers use to build applications. You learn what Express.js offers, how it works, and what you can do to customize it. In this unit, you also learn about the model-view-controller (MVC) application architecture pattern.
  • Unit 3 guides you through connecting your application to a database. This unit also helps you install a few new tools and structure your database with MongoDB.
  • Unit 4 teaches you about building data models in your application, where CRUD operations are performed to create, read, update, and delete data from the database.
  • Unit 5 helps you build code to represent user accounts in an object-oriented structure. In this unit, you learn about securing your data and building a login form for new users.
  • Unit 6 introduces building an application programming interface (API). You learn what constitutes an API, how to secure it, and how to design it by using the REST architecture.
  • Unit 7 invites you to build a live chat system into your application. This unit introduces polling, web sockets, and broadcasting data with Socket.io, a library that mainstream applications use to get data to their users faster and more efficiently.
  • Unit 8 guides you through the deployment process. This unit helps you set up the necessary tools and accounts.

To start, let’s talk a bit about exactly what Node.js is.

0.2. Understanding Node.js

Node.js is a platform for interpreting JavaScript code and running applications. Java-Script has been around for a couple of decades; with every improvement, it has shifted further from being a client-side scripting language to a full-fledged server-side programming language for managing data.

Because Node.js is built with Google Chrome’s JavaScript engine (a tool used to interpret the JavaScript language into meaningful computer commands), it’s considered to be powerful and able to support JavaScript as a server-side language. JavaScript can be used to both assist in web-page (client-side) interactions and handle incoming application data and database communications. (The latter jobs have often been reserved for languages such as C, Java, Python, and Ruby, among others). Developers can now commit to mastering JavaScript to build a complete web application instead of having to master multiple languages to accomplish the same task.

Client-side versus server-side

As a general overview, web development can largely be broken into two categories:

  • Client-side—(front-end) refers to the code you write that results in something the user sees in his web browser. Client-side code typically includes JavaScript used to animate the user experience when a web page is loaded.
  • Server-side—(back-end) refers to code used for application logic (how data is organized and saved to the database). Server-side code is responsible for authenticating users on a login page, running scheduled tasks, and even ensuring that the client-side code reaches the client.

In the following figure, the client represents the browser on which a user may view your application. The server is where your application runs and handles any data submitted by the user. Also, the server often renders the user interface in many cases in which the client expects one.

Note

Application, as used throughout this book, refers to a computer program written in a programming language and run on a computer. This book focuses on web applications written in JavaScript and run with Node.js.

Client-server interaction

You’ll hear these two terms used a lot in application development, and because Java-Script has been used for both types of development, the line separating these two worlds is disappearing. Full stack development, using JavaScript, defines this new development in which JavaScript is used on the server and client, as well as on devices, hardware, and architectures it didn’t exist on before.

Node.js operates on an event loop using a single thread. A thread is the bundle of computing power and resources needed for the execution of a programmed task. Generally, a thread is responsible for starting and completing a task; the more tasks needed to run simultaneously, the more threads are needed. In most other software, multiple tasks are matched and handled by a pool of threads that the computer can offer at the same time (concurrently). Node.js, however, handles only one task at a time and uses more threads only for tasks that can’t be handled by the main thread.

This process may sound counterintuitive, but in most applications that don’t require computationally intensive tasks (tasks requiring a lot of processing power from your computer), this single thread can quickly manage and execute all the tasks. See a simplified diagram of the event loop in figure 0.1. As tasks are prepared to run, they enter a queue to be processed by specific phases of the event loop.

Figure 0.1. Simplified model of the Node.js event loop

As its name implies, the Node.js event loop cycles forever in a loop, listening for JavaScript events triggered by the server to notify of some new task or another task’s completion. As the number of tasks increases, tasks line up in a queue to be incrementally processed by the event loop. You don’t code with this fact in mind, though. You write your code by using asynchronous conventions, and the Node.js architecture schedules task handling for you behind the scenes. As a result, Node.js has become popular for creating real-time applications that persistently listen for data being sent back and forth.

You can think of the event loop as being like an office manager. The office manager’s role is to handle incoming messages, job assignments, and office-related tasks. The office manager could have a long list of tasks to complete, from delegating the creation of complete financial reports to answering the phone and putting up the office-party decorations. Because some tasks take more time than others, the office manager isn’t obligated to complete any individual task before handling a new one. If she’s setting up for a party and the phone rings, for example, she can stop setting up to answer the call. Better yet, she can answer the call and transfer the caller to another employee so that she can go back to decorating.

Similarly, the event loop handles a series of tasks, always working on one task at a time and using the computer’s processing power to offload some larger tasks while the event loop shortens the list of tasks. On most other platforms, incoming tasks are assigned to new processes, creating a new event loop for each task. Increasing the number of tasks, however, is like increasing the number of employees in a finite space. You start to run into new issues such as cost, computing power, and shared resources. (What would you do if two employees need to use the phone at the same time, for example?)

Processes and threads

It’s important to note that the Node.js event loop relies on a single thread to manage all its tasks, but it doesn’t necessarily use that thread only to run each task to completion. In fact, Node.js is designed to pass larger tasks to the host computer, and the computer may create new threads and processes to operate those tasks.

A thread is an allocated bundle of computer resources used to run a series of instructions in a task. Usually, the tasks handled by threads are simple and fast. For this reason, the Node.js event loop needs only one thread to act as the manager of all other tasks. Threads are made available through computer processes, and some more-intensive tasks require their own process to run.

A process is also a bundle of computing power and resources used for a task’s execution, though usually for larger tasks than those handled by threads. A process must exist to create a thread, which implies that each Node.js application runs on its own process.

Even though Node.js may be single-threaded, you can have multiple instances of processes running in parallel and processing incoming requests and tasks. For this reason, Node.js scales well; it schedules tasks asynchronously, using additional threads and processes only when necessary instead of generating new processes for every task. As more processes are needed to handle your task list, demand on your computer increases. Node.js works best to minimize the number of concurrent processes.

You may hear the terms thread and process in conjunction. For this book, you only need to know that Node.js depends on a single task handler at any given time. For more information on threads and processes in Node.js, read the article on Node.js scalability at https://medium.freecodecamp.org/node-js-child-processes-everything-you-need-to-knowe69498fe970a.

In this book, I further explore some Node.js strengths in building a web application. Before I dive deeper, however, let’s talk about why Node.js is beneficial.

Quick check 0.1

Q1:

True or false: The Node.js event loop runs each task to completion before handling the next task.

QC 0.1 answer

1:

False. The Node.js event loop removes tasks from a queue sequentially, but it may offload the task to be handled by the machine on which the application is running or wait for certain tasks to complete while handling new tasks.

 

0.3. Why learn to develop in Node.js?

It’s likely that you’ve picked up this book to become a better programmer and to build a web application, which is also the main reason you’d use Node.js and get better at coding in JavaScript.

Plenty of other options, such as Ruby on Rails and PHP, could help you build an application that’s indistinguishable to a user from a Node.js application. Consider the following reasons to learn Node.js instead:

  • You can focus on JavaScript as the core language in development instead of balancing multiple languages to keep your application afloat.
  • If you want to stream data continuously or have some chat functionality, Node.js has gained prominence over other platforms.
  • Node.js is backed by Google’s V8 JavaScript interpreter, meaning that it’s widely supported and expected to grow in performance and features, and won’t go away soon. Visit http://node.green/ to see what features are supported by each version of Node.js.
  • Node.js has gained a lot of popularity in the web development community. You’re likely to meet and get support from other developers who may have been developing with Node.js for up to five years. Additionally, more supportive, open-source tools are now being built for Node.js than for other, older platforms.
  • More jobs are available for developers who have concrete JavaScript skills. You can apply for front-end or back-end development positions when you know Node.js.

If you’re trying to enter the web development world as a new programmer, or if you’ve developed software before and are looking for the new thing that everyone’s talking about, Node.js is your platform of choice, and this book is your book.

0.4. Preparing yourself for this book

From the first unit in this book, you’re introduced to web development through the process of building a basic web server in Node.js. As the book progresses, you’ll append code to your application to complete a robust web application.

To prime yourself to learn these new topics, make sure that you go through each lesson carefully and write all the code examples yourself. If you get into the habit of copying and pasting code instead, you’ll likely run into some errors; more important, you won’t learn the concepts.

Because JavaScript is an important prerequisite for this book, research best practices and other common solutions to your problems online if you struggle through a task. Throughout this book, you’ll find exercises and “Quick check” questions to test your knowledge. (You completed your first “Quick check” in section 2.) At the end of each lesson starting with lesson 3, expect a section called “Try this,” where you can practice some of the coding concepts presented earlier in the lesson.

The exercises and capstone projects at the end of each unit mark milestones on your way to creating a web application with all the bells and whistles.

Treat each unit like a course topic and each lesson as a lecture. You may find that some lessons take longer to comprehend or apply in code than others. Take your time, but also continually build your development skills through repetition and practice.

The goal of the book is to make you comfortable building a web application like the one built throughout the capstone lessons. In these capstones, you build a web application for a company called Confetti Cuisine, which offers cooking classes and allows users to sign up, connect, and chat with one another about recipes. Try to follow the guidelines of the capstone and redo part, or all, of the project after your first pass.

Tip

Consider working through an exercise three times. The first time, follow the guide; the second time, work with some reference from the guide; and the third time, work on your own without any help. By the third time, you’ll have a concrete understanding of the concept involved.

Most of the exercises in this book ask you to use your computer’s terminal (command line). Node.js is a cross-platform tool—meaning that it can run on Windows, Mac, and Linux machines—but I teach it from a UNIX perspective in this book. Windows users can use their built-in command line to run Node.js but may find some of the terminal commands to be different. As a result, I recommend that Windows users install Git Bash, a terminal window where you can use UNIX commands and follow all the examples in this book. You can accomplish a lot, however, with the Node.js command-line environment that comes with your Node.js installation. For information on installing Git Bash, visit https://git-scm.com/downloads.

After completing each unit, look back at the progress you’ve made since the last capstone exercise. By the end of unit 7, you’ll have built a complete web application with Node.js.

I’ll remind you about the following items along the way, but you should keep them in mind as you progress through this book:

  • Source files are written in JavaScript and have a .js file extension.
  • The main application file used in every example in the book is called main.js unless otherwise specified.
  • I recommend using an up-to-date Google Chrome browser for running book exercises that require a web browser. You can download that browser from https://www.google.com/chrome/browser/.

In the lessons I do my best to explain new terms and concepts tangential to the Node.js learning experience. If you need more information on any topic mentioned in the book, however, you can reference any of the following resources:

Summary

In this lesson, my objective was to teach you about the book’s structure, what Node.js is, and why it’s important. I also talked about how you should approach this book. If you treat this book as a course with subtopics and lectures, you’ll build your knowledge and skills incrementally until you become a competent web developer. In the next lesson, you install the tools that you need to get coding.

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

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