Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:12Length:1.9 hours

Next lesson playing in 5 seconds

Cancel

Free Preview: Node.js From Scratch

Introduction

01:50
  • Overview
  • Transcript

Node.js is an open-source, cross-platform JavaScript runtime environment that takes JavaScript into the server. Writing fast, cross-platform, and asynchronous back-ends with JavaScript is great. If you already use JavaScript on the front-end, Node.js makes it much easier to jump into full-stack app development.

Join Jeremy McPeak in this course as he teaches you the fundamentals of Node.js development. You'll learn the basic principles of Node, like the event loop and asynchronous programming. Then you'll learn how to use streams to read and write files, and how to create an HTTP server. You'll also learn some vital Node skills, like managing packages with NPM and working with the utilities in the util module.

Learn JavaScript: The Complete Guide

We've built a complete guide to help you learn JavaScript, whether you're just getting started as a web developer or you want to explore more advanced topics.

1. Introduction

1.1 Introduction

So you've heard about Node.js and the idea of writing fast, cross platform, and asynchronous applications with JavaScript is very intriguing. And I can relate. I love the JavaScript language, and being able to write web and desktop applications with JavaScript is, well, as I said, very intriguing. But I can also understand your hesitation. Especially if you're coming from a client-side web oriented background. Application development is very different than working within a browser. But then at the same time, there are a lot of similarities, and the fact that you already know JavaScript is a huge plus. Hi, my name is Jeremy McPeak, and I invite you to spend an hour and a half with me as I teach you the fundamental concepts and APIs of Node.js. We'll start at the very beginning. You will download and install Node.js, and then write a very simple test application. And from there we'll write an application that watches for changes to a file. And in doing so, you will learn several key concepts such as nodes event loop, using modules and how to write asynchronous code. You'll then learn other important concepts such as event emitters, streams, and error handling. And then we'll dive into streams, focusing on file streams. But you'll learn how to read and write data with them. And we'll use those concepts to write a simple but functional HTTP server. At the end of this course, you'll learn about Node package manager, or npm. And how you can use it to access hundreds of thousands of packages that you can use, not only in your applications, but in your daily work flow as well. We have a lot of ground to cover. So when you're ready, queue up the first video, and we will get started

1.2 Getting Set Up

The very first thing that we obviously need to do is make sure that we have Node on our system. Now, if you've done any type of client-side development, where you have used NPM or Yarn, then you have some version of Node. However, it might be an older version, and you might need to update that. So, if you go to the command line, type node --version, if you have Node installed, it's going to tell you what version you have. If you don't have Node, then, of course, this isn't going to work. But you can see here that I have version 7, which, there's a couple of problems. One of all, it's out of date. Second of all, this is what is, I'm not going to say it would be considered a beta version. However, there are essentially two versions of Node. We have what's called a long-term support version, which is something that you would want to use in production. And then there is what's called the current version, which is something that has all of the new features. So, it's not something that you would really want to use in support. Because these new features are being added in these current versions, where they're being tested and hardened and things like that. So, I need to update my installations, so I am going to go to nodejs.org, and we can see that we have two options. We have version eight, which is the LTS, long term support, which, as its name implies, it's long-term support. Then, there is the current version. Now, the current version right now is version 10. Usually, the even-numbered versions are the LTS versions. So, I imagine that version 10 will become LTS somewhat soon. If you look at the LTS schedule, they give you a little bit more detail as to when these things will come into play. So here, we have version 8, we can see that it was initially released about mid-2017. The active LTS for version 8 started in October of 2017, and it will last until December 2019. The current version of 9 was initially released, and of course there is no LTS support for the odd version numbers. But we can also see, for version ten, it was initially released in April. And then, the LTS will start in October of 2018. So, it will be the next LTS version. So when it comes to installing Node, it's very straightforward for Windows and for Mac. If we look at the other downloads, we have several options, 32 bit and 64 bit, and there are some Linux binaries. And of course, the source code is available if you wanted to compile it and then run it yourself. And in some Linux distributions, that's probably what you would need to do. But as far as the installation is concerned, you just take the defaults and you are essentially done. So, for Windows and for Mac, it's basically the same, of course, there's going to be differences as far as the UI is concerned. You will, of course, need to accept the license and terms, we'll take all of the defaults, and you definitely want to add Node to the path, because we use the command line a lot. And we also want to ensure that NPM is installed. Basically, remember, we want to take all of the defaults. So, we'll just click next until we can install, and then we get all of that stuff done. So, after this is installed, I'm gonna close my command line, so that all of the newer executables will be ready to go, as far as my path is concerned. And then we can get started with playing around with Node. Now, Node give us what's called a REPL, R-E-P-L. It stands for Read, Eval, Print, Loop. And if you have done any work with PHP or many different SQL servers, or SQL engines, I should say, and many other platforms, there is a way that you can go to the command line, you can run whatever command. And then you can start typing actual code, and it will read, and evaluate, and execute that code. We get the same type of environment with Node as well. So, once this is done, which it now is, let's fire up the command line. And then, we will play around with this. So the first thing I'm going to do is make sure that I have the latest version. So, node version, and we can see that I now have a 8.11.1. So to get to the REPL, all we have to do is just type node. And then, that's going to give us our interpreter here. So, Node is a JavaScript environment. So we can type, really, any JavaScript expression, and it will evaluate that. So we can see 1 + 2, and the output is three. We can also create variables. So, if we create a constant of foo and assign it hello world, then we see that it's undefined, but that's normal. We can type foo and we see the value there. So we can also create variables, or not variables, but functions. So, if we wanted to create a function called sum that accepts a and b, and then it simply returns a + b, we can easily do that. We can call sum, pass in 2 and 3, and we will see the value of 5. So, we can use Node itself to play around and test some ideas. It's nice whenever we need to quickly test the behavior of something, or just see how something is going to work. If you want to exit, you just say either Ctrl-C, or you can say Command-C if you're on a Mac. Or you can just say .exit, and it will exit out of the REPL. So, the last thing that we are going to do in this lesson is create a folder, and then create a file that is going to essentially be a Node application. So, going to make a directory. Let's just call this Node from scratch. You can call it whatever you want. And, then I'm going to fire up my code editor and I'm going to create a file. Let's just call this first.js. Let's go ahead and jump into strict mode, because that's always a good thing. And we are simply going to say console.log. Hello, Node.js!. And in order to run this, we will say node and then the name of our file. So that was first.js. And it will run that file. We see the result. Hello, NodeJS. And we are done. So, we have Node installed on our system. We also have played around with the REPL environment, as well as written our first very simple application. In the next lesson, we're going to, well, do a lot of the same thing. We're going to create a very simple JavaScript application that we can run with Node, but it will give us a chance to start diving into some of Node's APIs.