Learning Node.js Development
eBook - ePub

Learning Node.js Development

Andrew Mead

  1. 658 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

Learning Node.js Development

Andrew Mead

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

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.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Learning Node.js Development est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Learning Node.js Development par Andrew Mead en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Informatique et Programmation en JavaScript. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2018
ISBN
9781788396349

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...

Table des matiĂšres

  1. Title Page
  2. Copyright and Credits
  3. Packt Upsell
  4. Contributor
  5. Preface
  6. Getting Set Up
  7. Node Fundamentals – Part 1
  8. Node Fundamentals – Part 2
  9. Node Fundamentals – Part 3
  10. Basics of Asynchronous Programming in Node.js
  11. Callbacks in Asynchronous Programming
  12. Promises in Asynchronous Programming
  13. Web Servers in Node
  14. Deploying Applications to Web
  15. Testing the Node Applications – Part 1
  16. Testing the Node Applications – Part 2
  17. Another Book You May Enjoy