Learning Node.js Development
eBook - ePub

Learning Node.js Development

Andrew Mead

Buch teilen
  1. 658 Seiten
  2. English
  3. ePUB (handyfreundlich)
  4. Über iOS und Android verfügbar
eBook - ePub

Learning Node.js Development

Andrew Mead

Angaben zum Buch
Buchvorschau
Inhaltsverzeichnis
Quellenangaben

Über dieses Buch

A comprehensive, easy-to-follow guide to creating complete Node apps and understanding how to build, deploy, and test your own apps.About This Book• Entirely project-based and practical• Explains the "Why" of Node.js features, not just the "how", providing you with a deep understanding and enabling you to easily apply concepts in your own applications• Covers the full range of technologies around Node.js – NPM, version control with Git, and much moreWho This Book Is ForThis book targets anyone looking to launch their own Node applications, switch careers, or freelance as a Node developer. You should have a basic understanding of JavaScript in order to follow this course.What You Will Learn• Learn the fundamentals of Node• Build apps that respond to user input• Master working with servers• Learn how to test and debug applications• Deploy and update your apps in the real world• Create responsive asynchronous web applicationsIn DetailLearning Node.js Development is a practical, project-based book that provides you with all you need to get started as a Node.js developer. Node is a ubiquitous technology on the modern web, and an essential part of any web developers' toolkit. If you are looking to create real-world Node applications, or you want to switch careers or launch a side project to generate some extra income, then you're in the right place. This book has been written around a single goal—turning you into a professional Node developer capable of developing, testing, and deploying real-world production applications. Learning Node.js Development is built from the ground up around the latest version of Node.js (version 9.x.x). You'll be learning all the cutting-edge features available only in the latest software versions.This book cuts through the mass of information available around Node and delivers the essential skills that you need to become a Node developer. It takes you through creating complete apps and understanding how to build, deploy, and test your own Node apps. It maps out everything in a comprehensive, easy-to-follow package designed to get you up and running quickly.Style and approachThis book is entirely project-based. From the very beginning you'll be programming every single app and completing various challenges designed to help test and reinforce what you've learned. There's no copying-and-pasting here. This book is about writing code and building projects.

Häufig gestellte Fragen

Wie kann ich mein Abo kündigen?
Gehe einfach zum Kontobereich in den Einstellungen und klicke auf „Abo kündigen“ – ganz einfach. Nachdem du gekündigt hast, bleibt deine Mitgliedschaft für den verbleibenden Abozeitraum, den du bereits bezahlt hast, aktiv. Mehr Informationen hier.
(Wie) Kann ich Bücher herunterladen?
Derzeit stehen all unsere auf Mobilgeräte reagierenden ePub-Bücher zum Download über die App zur Verfügung. Die meisten unserer PDFs stehen ebenfalls zum Download bereit; wir arbeiten daran, auch die übrigen PDFs zum Download anzubieten, bei denen dies aktuell noch nicht möglich ist. Weitere Informationen hier.
Welcher Unterschied besteht bei den Preisen zwischen den Aboplänen?
Mit beiden Aboplänen erhältst du vollen Zugang zur Bibliothek und allen Funktionen von Perlego. Die einzigen Unterschiede bestehen im Preis und dem Abozeitraum: Mit dem Jahresabo sparst du auf 12 Monate gerechnet im Vergleich zum Monatsabo rund 30 %.
Was ist Perlego?
Wir sind ein Online-Abodienst für Lehrbücher, bei dem du für weniger als den Preis eines einzelnen Buches pro Monat Zugang zu einer ganzen Online-Bibliothek erhältst. Mit über 1 Million Büchern zu über 1.000 verschiedenen Themen haben wir bestimmt alles, was du brauchst! Weitere Informationen hier.
Unterstützt Perlego Text-zu-Sprache?
Achte auf das Symbol zum Vorlesen in deinem nächsten Buch, um zu sehen, ob du es dir auch anhören kannst. Bei diesem Tool wird dir Text laut vorgelesen, wobei der Text beim Vorlesen auch grafisch hervorgehoben wird. Du kannst das Vorlesen jederzeit anhalten, beschleunigen und verlangsamen. Weitere Informationen hier.
Ist Learning Node.js Development als Online-PDF/ePub verfügbar?
Ja, du hast Zugang zu Learning Node.js Development von Andrew Mead im PDF- und/oder ePub-Format sowie zu anderen beliebten Büchern aus Ciencia de la computación & Programación en JavaScript. Aus unserem Katalog stehen dir über 1 Million Bücher zur Verfügung.

Information

Node Fundamentals – Part 1

In this chapter, you'll learn a ton about building Node applications, and you'll actually build your first Node application. This is where all the really fun stuff is going to start.
We'll kick things off by learning about all of the modules that come built in to Node. These are objects and functions that let you do stuff with JavaScript you've never been able to do before. We'll learn how to do things, such as reading and writing from the filesystem, which we'll use in the Node's application to persist our data.
We'll also be looking at third-party npm modules; this is a big part of the reason that Node became so popular. The npm modules give you a great collection of third-party libraries you can use, and they also have really common problems. So you don't have to rewrite that boilerplate code over and over again. We'll be using a third-party module in this chapter to help with fetching input from the user.
The chapter will specifically cover the following topics:
  • Module basics
  • Require own files
  • Third-party modules
  • Global modules
  • Getting input

Module basics

In this section, you will finally learn some Node.js code, and we'll kick things off by talking about modules inside Node. Modules are units of functionality, so imagine I create a few functions that do something similar, such as a few functions that help with math problems, for example, add, subtract, and divide. I could bundle those up as a module, call it Andrew-math, and other people could take advantage of it.
Now, we'll not be looking at how to make our own module; in fact, we will be looking at how we can use modules, and that will be done using a function in Node, called require(). The require() function will let us do three things:
  • First, it'll let us load in modules that come bundled with Node.js. These include the HTTP module, which lets us make a web server, and the fs module, which lets us access the filesystem for our machine.
We will also be using require() in later sections to load in third-party libraries, such as Express and Sequelize, which will let us write less code.
  • We'll be able to use prewritten libraries to handle complex problems, and all we need to do is implement require() by calling a few methods.
  • We will use require() to require our very own files. It will let us break up our application into multiple, smaller files, which is essential for building real-world apps.
If you have all of your code in one file, it will be really hard to test, maintain, and update. Now, require() isn't that bad. In this section, we'll explore the first use case for require().

Using case for require()

We'll take a look at two built-in modules; we'll figure out how to require them and how to use them, and then we'll move on to starting the process of building that Node application.

Initialization of an application

The first step we'll take inside of the Terminal is that we'll make a directory to store all of these files. We'll navigate from our home directory to the desktop using the cd Desktop command:
cd Desktop
Then, we'll make a folder to store all of the lesson files for this project.
Now, these lesson files will be available in the resources section for every section, so if you get stuck or your code just isn't working for some reason, you can download the lesson files, compare your files, and figure out where things went wrong.
Now, we'll make that folder using the mkdir command, which is the short form for make directory. Let's call the folder notes-node, as shown in the following code:
mkdir notes-node
We'll make a note app in Node so that notes-node seems appropriate. Then we'll cd into notes-node, and we can get started playing around with some of the built-in modules:
cd notes-node
These modules are built in, so there's no need to install anything in Terminal. We can simply require them right inside of our Node files.
The next step in the process is to open up that directory inside the Atom text editor. So open up the directory we just created on the Desktop, and you will find it there, as shown in the following screenshot:
Now, we will need to make a file, and we'll put that file in the root of the project:
We'll call this file app.js, and this is where our application will start:
We will be writing other files that get used throughout the app, but this is the only file we'll ever be running from Terminal. This is the initialization file for our application.

The built-in module to use require()

Now, to kick things off, the first thing I will do is to use console.log to print Starting app, as shown in the following code:
console.log('Starting app');
The only reason we'll do this is to keep track of how our files are executing, and we'll do this only for the first project. Down the line, once you're comfortable with how files get loaded and how they run, we'll be able to remove these console.log statements, as they won't be necessary.
After we call the console.log starting app, we'll load in a built-in module using require().
We can get a complete list of all of the built-in modules in the Node.js API docs.
To view Node.js API docs, go to nodejs.org/api. When you go to this URL, you'll be greeted with a long list of built-in modules. Using the File System module we'll create a new file and the OS module. The OS module will let us fetch things such as the username for the currently logged-in user.

Creating and appending files in the File System module

To kick things off though, we will start with the File System module. We'll go through the process of creating a file and appending to it:
When you view a docs page for a built-in module, whether it's File System or a different module, you'll see a long list of all the different functions and properties that you have available to you. The one we'll use in this section is fs.appendFile.
If you click on it, it will take you to the specific documentation, and this is where we can figure out how to use appendFile, as shown in the following screenshot:
Now, appendFile is pretty simple. We'll pass to it two string arguments (shown in the preceding screenshot):
  • One will be the file name
  • The other will be the data we want to append to the file
This is all we need to provide in order to call fs.appendFile. Before we can call fs.appendFile, we need to require it. The whole point of requiring is to let us load in other modules. In this case, we'll load in the fs module from app.js.
Let's create a variable that will be a constant, using const.
Since we'll not be manipulating the code the module sends back, there's no need to use the v...

Inhaltsverzeichnis