Why learn Node.js in 2020? Master the fundamentals of Node in 7 easy steps, then build a fullstack web app and deploy it to a cloud server. Follow along in the full article: https://fireship.io/courses/javascript/node-basics/

1. What is Node?
2. How do you install Node?
3. Hello World
4. Know the Runtime
5. Events
6. File System
7. Modules
8. Build & Deploy 

Node https://nodejs.org

#nodejs #javascript #tutorial 

Install the quiz app 🤓

iOS https://itunes.apple.com/us/app/fireship/id1462592372?mt=8
Android https://play.google.com/store/apps/details?id=io.fireship.quizapp

Upgrade to Fireship PRO at https://fireship.io/pro
Use code lORhwXd2 for 25% off your first payment. 

My VS Code Theme

- Atom One Dark 
- vscode-icons
- Fira Code Font
2024-02-04T06:12:13.125Z
nova-2
2-general-nova
2024-01-09.29447
30089e05-99d1-4376-b32e-c263170674af
b3365ba3-4a62-4332-acaf-d15e43666f52
3514ee7ec0c09c40b46d70bc8c95528f438783fa32d62fa10736885549e97b04
67875a7f-c9c4-48a0-aa55-5bdb8a91c34a
deprecated
One of the most valuable skills to have as a full stack web developer is, without a doubt, Node.
Js, and that's not going to change anytime soon.
But you might be wondering why I'm making a Node tutorial in 2020.
Because if you've been watching the news, you may have heard that Node was killed last week by Deno.
When I lay my vengeance upon me.
Making the extremely chaotic JavaScript world even more convoluted.
If you're just starting to learn JavaScript, this can be very confusing, and you might be wondering, should I learn Node or Deno?
The answer is Node, especially if you want to get a job or build a product.
When something becomes as established as Node, it doesn't get replaced.
You'll see things like this happen all the time.
A few years ago, everybody was saying GraphQL would replace REST.
And 10 years ago, people were saying Node.
Js would replace PHP.
These predictions never pan out and are just built on top of hype.
That being said, I do think Deno is awesome and has a bright future.
Both.
Both.
Both.
Both is good.
And if you learn Node today, those skills will mostly transfer to Deno tomorrow.
In today's video, you'll learn the basics of Node.
Js in 7 easy steps.
Then we'll put those steps together to build a full stack web application.
Because in my opinion, the best way to learn how to code is to build something meaningful from scratch.
By the end of the video, we'll deploy our code to the cloud so you can show off your API to your friends and family.
If you're new here, like and subscribe and head over to FireShip IO to follow along with the full write up to this video.
Step 1 is to understand what Node JS is and why you would wanna use it.
Node.
Js itself is not a programming language, but rather a runtime that allows you to run JavaScript on a server.
You see, when JavaScript first came around in the 19 nineties, It was designed as a simple scripting language to run-in the browser.
As the web platform evolved, JavaScript became more and more powerful.
And in 2009, we saw the initial release of Node.
Js.
Now up until this time, it was impossible to write JavaScript code on the server.
Most servers were written in languages like Java or PHP.
It revolutionized web development because now a web developer could write a full stack application in a single language.
So that's all Node.
Js is.
It's a way for developers to write JavaScript on the server when it could previously only be written in a web browser.
Now there are a lot of low level implementation details here that you don't need to know about as a beginner.
But if you really wanna dive into how Node.
Js works, check out my how it's made video.
But what kind of problems can Node.
Js actually solve?
Node.
Js can do a lot.
So instead of listing out a bunch of use cases, I'll show you how to build a full stack web application from scratch.
This is how it'll work.
You visit a URL on the Internet that points to your server.
When the request is received, we can use Node to handle the request and read a file from the server's file system and then respond back to the client so they can view the HTML in the browser.
Step 2, how to install Node.
Js.
Node can be installed on Windows, Mac, or Linux and is probably already installed on your system.
In this tutorial, I'll be using Ubuntu via the Windows subsystem for Linux.
At this point, I've just opened up an empty directory in Versus Code and then pulled up a terminal session.
You can find out if Node is installed on your system by running node flag v from the command line.
As you can see here, I'm running version 12 dot 16, which is the current long term support version.
If If you get an error at this point, it means that you don't have Node installed.
And even if you do have Node installed, it'd be a very good idea to use a package called Node Version manager to manage the installation of different node versions on your system.
There's an n v m package for Mac and Linux or a separate package for Windows.
Go ahead and follow the instructions to install NVM on your system, and that will give you the ability to install any version of Node.
Js that you want.
For example, you could run nvm install 12 dot 16 dot 3, then run nvm use to use that specific version as your runtime.
And now that we have Node JS installed, we can move on to step 3, hello world.
But first, if you're serious about Node.
Js, consider becoming a pro member at FireShip IO.
It'll give you access to my full length courses, almost all of which cover advanced use cases for Node.
Js, but we need to walk before we can run.
So how do we actually use this thing?
One way to mess around with Node is in Repel mode, which stands for read eval print loop.
When you type node into the command line, it allows you to run JavaScript code and will print out the results.
For example, we can type console log hello world, and it will log that value out.
This is nice for messing around, but go ahead and hit control c twice to shut it down.
Because in most cases, we'll want to execute JavaScript code that lives in an actual file.
The default entry point into a Node.
Js app is the index dot JS file.
Go ahead and create an index JS file manually in Versus Code.
Then inside that file, add console log hello world.
You can now run the code inside this file with the node command followed by the path to this file.
And because it's an index file, you can actually just point to the parent directory.
Congratulations.
You just built a node app.
Step 4, understand the Node runtime.
In most ways, JavaScript works the same way it does in the browser as it does Node.
Js, but there are some very important differences you should know about.
First, node has a handful of built in identifiers.
One of those globals is console, which we've already been using to log out values to the terminal.
There's another global with a name of global.
This object is a namespace that is available throughout the entire node process.
For example, if we assigned a lucky number property to global, We could then access it anywhere else in our code.
If you're a front end web developer, you can compare global to the window object in the browser.
Now I think the most important global to be familiar with is process, which gives you access to the currently running node process.
You might use it to check the current platform or operating system, which in my case is Linux, or grab an environment variable from your server.
But this gives us a great opportunity to segue into step 5, how do events work in Node.
Js?
You'll often hear people describe it as an asynchronous event driven JavaScript runtime.
The runtime implements a thing called an event loop, just like a web browser does, and it allows Node to push intensive operations off to a separate thread So only very fast non blocking operations happen on the main thread.
And this is why people often call Node non blocking.
It's a design choice that makes Node.
Js very suitable for things that require high throughput like real time applications, web servers, and things like that.
Okay.
So how does that affect me as a coder?
Well, normally, you won't have to think about the low level details.
You just need to know how events and callbacks work.
In most cases, you'll listen to events.
Events can come in many forms, but one example is on the process global that we looked at earlier.
Before a node process finishes, it emits an event named exit.
We can listen to this event using on and then register a callback function as the second argument.
When the exit event occurs, Node.
Js will run this function, and that's where it gets the name callback because the function isn't called the first time Node JS sees it.
It's only called after the exit event occurs at some point in the future.
This event is built into Node, but you can also create your own from scratch.
We'll import an event emitter from the events module that is built into Node, and we'll look at modules in more detail in just a minute.
We can create a custom event emitter by instantiating the class and then we'll register a callback that fires on the lunch event.
Now that the callback is registered, You can simply call event emitter emit with that event name, and that triggers your callback function to run.
As you can see here, we emit the event twice, which will run the callback function twice.
This event driven style of programming is extremely useful when you have something that is CPU intensive.
And with that, we can move on to step 6, the file system.
Node has a built in file system module called FS.
It can read, write, and delete files on the file system, among other things, and it can also do things in a blocking or non blocking way.
Allow me to explain that by showing you an example.
1st, create a file on the file system called hello dot txt, and then add whatever text you want inside of it.
In our JavaScript code, we'll import 2 functions from the file system module called read file and read file sync.
Anytime you see a function that ends in sync, think blocking.
Or in other words, it will need to finish all of its work before any of your other code can run.
We can read a text file in node by simply passing the path to that file, and then we'll specify the encoding as utf8.
Now reading a file might take a while, especially if it's a very large file.
And what you'll notice here when we run our code is that the second console log won't run until after that file has been read.
Luckily, you can make your code non blocking by refactoring this to a callback.
With read file, we pass the same first two arguments and then add a callback function as the 3rd argument.
Inside the function, we can access an error object if the operation fails or when successful, the actual text from the file.
The super cool thing about this is that even though the console log to the text file comes first in our script, it's not the thing that gets executed first.
Node registers the callback, executes the rest of the script, and then finally runs the callback when the file has been read.
So that gives us 2 different ways to read a file, But there's actually one other way we could go about this, and that's using a Promise based solution.
Promises are also asynchronous and non blocking, and they tend to produce much cleaner code when compared to callbacks.
Notice how in this example, we're importing ReadFile from the Promises namespace.
This gives us a function that returns a Promise when called.
If you're using the latest bleeding edge version of node, you could potentially use this function with top level await, which means you can use await to resolve the promise here at the top of the script.
However, since we're currently using Node version 12, we'll go ahead and wrap this in an async function.
And this async await syntax will make your code much easier to read, especially when you have multiple async calls in the same function.
Okay.
So now that you know how to read files, I want to backtrack a little bit and talk about this require function that you see me use in the past couple examples.
Step 7, modules.
A module is nothing more than a JavaScript file that exports its code.
Node has a bunch of built in modules like f s and events that we've already looked at, and there's a long list of other modules beyond that.
The traditional way to import a module in Node is to use this require function.
But a quick side note on that first because Node recently added support for ES modules, which use the import export syntax.
Most Node.
Js code out there written in vanilla JavaScript still uses require, so it's very important for you to know as a Node.
Js developer.
And if all that's confusing to you, Well, all I can say is welcome to the JavaScript world.
Let's look at how we can use modules in our own codebase.
Create a new file to serve as your module, Then go into your index JS file, create a variable for the module, and then import it using require.
When you console log it, you'll notice that it's currently just an empty object.
In order to make a module useful, you need to export some code from it.
In the module's file, you can reference this object with module dot exports.
You can add new properties to the object or redefine it as a new object.
In either case, whatever you add here will now be available to use in the other file.
And as you can see, the object is no longer empty when we console log it.
But at some point, you'll very likely want to use somebody else's code out there in the world.
And the primary place to do that is via Node Package Manager or NPM, which was recently acquired by GitHub, which itself was recently acquired by Microsoft.
When you installed Node earlier in the video, it also installed npm, which is a tool you can use to install remote packages to use in your own code.
The first step we'll want to take is to open the command line and run npm init.
We'll use the y flag to use the default options, and what you'll notice is that it creates a package JSON file here in the root of the application.
This file can contain metadata about your project, but most importantly, it keeps track of the dependencies that you use here.
At this point, we haven't installed anything yet.
So let's go ahead and change that by opening the command line again and running npm install express.
Express is a minimal web application framework and one of the most popular third party node modules.
After running the command, you'll notice a few things happened here.
In the package JSON file, it added express to the dependencies and pegged it to a specific version.
This dependencies object allows you to manage multiple dependencies in a project and then reinstall them all at once on a different system.
Now the actual raw source code for the dependency lives in this node modules directory.
You should never have to modify code in the node modules directory.
If you find yourself writing code in here, you're probably doing it wrong.
That's because the package JSON controls how this directory is built.
It fetches your remote packages, Saves the source code here and that process should be able to be repeated on any system.
Now that we have this package installed, we can import it by name in our JavaScript code.
In this case, we simply require Express.
And now we've reached the point where we're ready to put these 7 steps together to build a real full stack application and deploy it to the cloud.
What we're building here is actually just your typical full stack web application.
Our server will live on a URL, and when a user makes a request to this URL in the browser, The server will respond with some HTML.
In our code, we'll first create an instance of an Express app.
An Express app allows us to Create different URLs and endpoints that a user could navigate to in the browser.
And then we define code for the server to handle those requests.
Now I don't wanna get too deep into HTTP, but when the user navigates to a URL in the browser, it's what's known as a get request, which means they're requesting some data on the server and not trying to modify or update anything on the server.
With Express, we can set up an endpoint like that by calling app dot get.
And then the first argument is the URL that the user will navigate to.
In this case, we'll just use a forward slash for the root URL.
But feel free to create multiple pages for your web app by creating different URLs.
The second argument here is our callback function.
You can think of every request to this URL as an event, and then you handle that event with this function.
And Express gives us 2 parameters to make use of, the request and the response.
The request is the incoming data from the user.
In this example, we don't really need to parse any data from the request.
However, in many cases, you might wanna look at the headers or the body of the request to authenticate a user or understand what the user is trying to do.
At this point, we need to implement the code to handle the request.
What we wanna do is read some HTML from our file system and then send it back down to the browser.
As you can see here, I'm creating a file called home dot HTML with just some generic HTML markup inside.
In our source code, we can then import read file from Node's file system module.
Just like we did earlier, we'll read the file, use UTF aid encoding.
Then in our callback function here, we'll have access to the HTML string, and we can send a response back down to the client by calling response dot send.
And, also, if there's an error in the callback, we can handle that by sending a response with a status code of 500, which means a server error, so the user knows that something went wrong on the server.
And that's all there is to it.
We now have a way to send HTML from the server to the client.
Now we just need to tell our Express app to start listening to incoming requests.
We do that by defining a port, which will normally come from a node environment variable.
Then we call app dot listen with that port, and when it starts up, we'll console log that the app is available on local host 3,000.
You You can start it up by opening the command line and running Node with the current working directory.
If you go ahead and open it in the browser, you should see your HTML return back to you.
Now there is one important thing you should know at this point, and that's that callbacks can be very difficult to work with, especially as your app grows in complexity.
It often leads to a state known as callback hell where you have a bunch of callbacks nested within callbacks, within more callbacks, and so on.
A great way to avoid code like this is to use promises instead of callbacks, and that's very easy to do in Node.
Js.
Instead of importing read file from f s, We'll import it from f s dot promises.
We can make our callback function async, and then we can write the response in a single line of code by saying response send and then await the operation to read file.
That's much more concise and readable, but it's especially useful when you have multiple async operations to handle in a single request.
So So now that we've built a Node.
Js app, how do we deploy it to the cloud so people can actually use it?
There's a bunch of different ways we could do it, but an easy and free way is with Google App Engine.
App Engine has what's known as a standard environment for Node.
Js up to version 12.
And what that does is provide you with a server in the cloud that scales automatically based on the incoming graphic to your app.
It's incredibly easy to set up, but you first need to have a Google Cloud Platform account and also the Google Cloud command line tools installed on your local system.
Once you have that done, you can simply go into your source code and create an app dot YAML file.
This file is used to configure your cloud server.
The only thing we need at this point is to specify a runtime of Node JS version 12.
App Engine will run your code by looking in the package JSON file for a start script.
So let's go in there and define a start script that runs node in the current working directory to start our Express app.
From there, we can simply open the command line and run gcloud app deploy.
That'll take a minute, and then it will give you a URL where you can access your app publicly on the web.
Congratulations.
You're now a full stack cloud architect.
If this video helped you, please like and subscribe.
And if you really want to dive deep into Node.
Js and Express, consider becoming a pro member at FireShip IO.
I have a whole bunch of advanced content covering real world use cases with these technologies.
Thanks for watching, and I will see you in the next one.

Speaker 0: One of the most valuable skills to have as a full stack web developer is, without a doubt, Node. Js, and that's not going to change anytime soon. But you might be wondering why I'm making a Node tutorial in 2020. Because if you've been watching the news, you may have heard that Node was killed last week by Deno. When I lay my vengeance upon me.

Making the extremely chaotic JavaScript world even more convoluted. If you're just starting to learn JavaScript, this can be very confusing, and you might be wondering, should I learn Node or Deno? The answer is Node, especially if you want to get a job or build a product. When something becomes as established as Node, it doesn't get replaced. You'll see things like this happen all the time.

A few years ago, everybody was saying GraphQL would replace REST. And 10 years ago, people were saying Node. Js would replace PHP. These predictions never pan out and are just built on top of hype. That being said, I do think Deno is awesome and has a bright future.

Both. Both. Both. Both is good. And if you learn Node today, those skills will mostly transfer to Deno tomorrow.

In today's video, you'll learn the basics of Node. Js in 7 easy steps. Then we'll put those steps together to build a full stack web application. Because in my opinion, the best way to learn how to code is to build something meaningful from scratch. By the end of the video, we'll deploy our code to the cloud so you can show off your API to your friends and family.

If you're new here, like and subscribe and head over to FireShip IO to follow along with the full write up to this video. Step 1 is to understand what Node JS is and why you would wanna use it. Node. Js itself is not a programming language, but rather a runtime that allows you to run JavaScript on a server. You see, when JavaScript first came around in the 19 nineties, It was designed as a simple scripting language to run-in the browser.

As the web platform evolved, JavaScript became more and more powerful. And in 2009, we saw the initial release of Node. Js. Now up until this time, it was impossible to write JavaScript code on the server. Most servers were written in languages like Java or PHP.

It revolutionized web development because now a web developer could write a full stack application in a single language. So that's all Node. Js is. It's a way for developers to write JavaScript on the server when it could previously only be written in a web browser. Now there are a lot of low level implementation details here that you don't need to know about as a beginner.

But if you really wanna dive into how Node. Js works, check out my how it's made video. But what kind of problems can Node. Js actually solve? Node.

Js can do a lot. So instead of listing out a bunch of use cases, I'll show you how to build a full stack web application from scratch. This is how it'll work. You visit a URL on the Internet that points to your server. When the request is received, we can use Node to handle the request and read a file from the server's file system and then respond back to the client so they can view the HTML in the browser.

Step 2, how to install Node. Js. Node can be installed on Windows, Mac, or Linux and is probably already installed on your system. In this tutorial, I'll be using Ubuntu via the Windows subsystem for Linux. At this point, I've just opened up an empty directory in Versus Code and then pulled up a terminal session.

You can find out if Node is installed on your system by running node flag v from the command line. As you can see here, I'm running version 12 dot 16, which is the current long term support version. If If you get an error at this point, it means that you don't have Node installed. And even if you do have Node installed, it'd be a very good idea to use a package called Node Version manager to manage the installation of different node versions on your system. There's an n v m package for Mac and Linux or a separate package for Windows.

Go ahead and follow the instructions to install NVM on your system, and that will give you the ability to install any version of Node. Js that you want. For example, you could run nvm install 12 dot 16 dot 3, then run nvm use to use that specific version as your runtime. And now that we have Node JS installed, we can move on to step 3, hello world. But first, if you're serious about Node.

Js, consider becoming a pro member at FireShip IO. It'll give you access to my full length courses, almost all of which cover advanced use cases for Node. Js, but we need to walk before we can run. So how do we actually use this thing? One way to mess around with Node is in Repel mode, which stands for read eval print loop.

When you type node into the command line, it allows you to run JavaScript code and will print out the results. For example, we can type console log hello world, and it will log that value out. This is nice for messing around, but go ahead and hit control c twice to shut it down. Because in most cases, we'll want to execute JavaScript code that lives in an actual file. The default entry point into a Node.

Js app is the index dot JS file. Go ahead and create an index JS file manually in Versus Code. Then inside that file, add console log hello world. You can now run the code inside this file with the node command followed by the path to this file. And because it's an index file, you can actually just point to the parent directory.

Congratulations. You just built a node app. Step 4, understand the Node runtime. In most ways, JavaScript works the same way it does in the browser as it does Node. Js, but there are some very important differences you should know about.

First, node has a handful of built in identifiers. One of those globals is console, which we've already been using to log out values to the terminal. There's another global with a name of global. This object is a namespace that is available throughout the entire node process. For example, if we assigned a lucky number property to global, We could then access it anywhere else in our code.

If you're a front end web developer, you can compare global to the window object in the browser. Now I think the most important global to be familiar with is process, which gives you access to the currently running node process. You might use it to check the current platform or operating system, which in my case is Linux, or grab an environment variable from your server. But this gives us a great opportunity to segue into step 5, how do events work in Node. Js?

You'll often hear people describe it as an asynchronous event driven JavaScript runtime. The runtime implements a thing called an event loop, just like a web browser does, and it allows Node to push intensive operations off to a separate thread So only very fast non blocking operations happen on the main thread. And this is why people often call Node non blocking. It's a design choice that makes Node. Js very suitable for things that require high throughput like real time applications, web servers, and things like that.

Okay. So how does that affect me as a coder? Well, normally, you won't have to think about the low level details. You just need to know how events and callbacks work. In most cases, you'll listen to events.

Events can come in many forms, but one example is on the process global that we looked at earlier. Before a node process finishes, it emits an event named exit. We can listen to this event using on and then register a callback function as the second argument. When the exit event occurs, Node. Js will run this function, and that's where it gets the name callback because the function isn't called the first time Node JS sees it.

It's only called after the exit event occurs at some point in the future. This event is built into Node, but you can also create your own from scratch. We'll import an event emitter from the events module that is built into Node, and we'll look at modules in more detail in just a minute. We can create a custom event emitter by instantiating the class and then we'll register a callback that fires on the lunch event. Now that the callback is registered, You can simply call event emitter emit with that event name, and that triggers your callback function to run.

As you can see here, we emit the event twice, which will run the callback function twice. This event driven style of programming is extremely useful when you have something that is CPU intensive. And with that, we can move on to step 6, the file system. Node has a built in file system module called FS. It can read, write, and delete files on the file system, among other things, and it can also do things in a blocking or non blocking way.

Allow me to explain that by showing you an example. 1st, create a file on the file system called hello dot txt, and then add whatever text you want inside of it. In our JavaScript code, we'll import 2 functions from the file system module called read file and read file sync. Anytime you see a function that ends in sync, think blocking. Or in other words, it will need to finish all of its work before any of your other code can run.

We can read a text file in node by simply passing the path to that file, and then we'll specify the encoding as utf8. Now reading a file might take a while, especially if it's a very large file. And what you'll notice here when we run our code is that the second console log won't run until after that file has been read. Luckily, you can make your code non blocking by refactoring this to a callback. With read file, we pass the same first two arguments and then add a callback function as the 3rd argument.

Inside the function, we can access an error object if the operation fails or when successful, the actual text from the file. The super cool thing about this is that even though the console log to the text file comes first in our script, it's not the thing that gets executed first. Node registers the callback, executes the rest of the script, and then finally runs the callback when the file has been read. So that gives us 2 different ways to read a file, But there's actually one other way we could go about this, and that's using a Promise based solution. Promises are also asynchronous and non blocking, and they tend to produce much cleaner code when compared to callbacks.

Notice how in this example, we're importing ReadFile from the Promises namespace. This gives us a function that returns a Promise when called. If you're using the latest bleeding edge version of node, you could potentially use this function with top level await, which means you can use await to resolve the promise here at the top of the script. However, since we're currently using Node version 12, we'll go ahead and wrap this in an async function. And this async await syntax will make your code much easier to read, especially when you have multiple async calls in the same function.

Okay. So now that you know how to read files, I want to backtrack a little bit and talk about this require function that you see me use in the past couple examples. Step 7, modules. A module is nothing more than a JavaScript file that exports its code. Node has a bunch of built in modules like f s and events that we've already looked at, and there's a long list of other modules beyond that.

The traditional way to import a module in Node is to use this require function. But a quick side note on that first because Node recently added support for ES modules, which use the import export syntax. Most Node. Js code out there written in vanilla JavaScript still uses require, so it's very important for you to know as a Node. Js developer.

And if all that's confusing to you, Well, all I can say is welcome to the JavaScript world. Let's look at how we can use modules in our own codebase. Create a new file to serve as your module, Then go into your index JS file, create a variable for the module, and then import it using require. When you console log it, you'll notice that it's currently just an empty object. In order to make a module useful, you need to export some code from it.

In the module's file, you can reference this object with module dot exports. You can add new properties to the object or redefine it as a new object. In either case, whatever you add here will now be available to use in the other file. And as you can see, the object is no longer empty when we console log it. But at some point, you'll very likely want to use somebody else's code out there in the world.

And the primary place to do that is via Node Package Manager or NPM, which was recently acquired by GitHub, which itself was recently acquired by Microsoft. When you installed Node earlier in the video, it also installed npm, which is a tool you can use to install remote packages to use in your own code. The first step we'll want to take is to open the command line and run npm init. We'll use the y flag to use the default options, and what you'll notice is that it creates a package JSON file here in the root of the application. This file can contain metadata about your project, but most importantly, it keeps track of the dependencies that you use here.

At this point, we haven't installed anything yet. So let's go ahead and change that by opening the command line again and running npm install express. Express is a minimal web application framework and one of the most popular third party node modules. After running the command, you'll notice a few things happened here. In the package JSON file, it added express to the dependencies and pegged it to a specific version.

This dependencies object allows you to manage multiple dependencies in a project and then reinstall them all at once on a different system. Now the actual raw source code for the dependency lives in this node modules directory. You should never have to modify code in the node modules directory. If you find yourself writing code in here, you're probably doing it wrong. That's because the package JSON controls how this directory is built.

It fetches your remote packages, Saves the source code here and that process should be able to be repeated on any system. Now that we have this package installed, we can import it by name in our JavaScript code. In this case, we simply require Express. And now we've reached the point where we're ready to put these 7 steps together to build a real full stack application and deploy it to the cloud. What we're building here is actually just your typical full stack web application.

Our server will live on a URL, and when a user makes a request to this URL in the browser, The server will respond with some HTML. In our code, we'll first create an instance of an Express app. An Express app allows us to Create different URLs and endpoints that a user could navigate to in the browser. And then we define code for the server to handle those requests. Now I don't wanna get too deep into HTTP, but when the user navigates to a URL in the browser, it's what's known as a get request, which means they're requesting some data on the server and not trying to modify or update anything on the server.

With Express, we can set up an endpoint like that by calling app dot get. And then the first argument is the URL that the user will navigate to. In this case, we'll just use a forward slash for the root URL. But feel free to create multiple pages for your web app by creating different URLs. The second argument here is our callback function.

You can think of every request to this URL as an event, and then you handle that event with this function. And Express gives us 2 parameters to make use of, the request and the response. The request is the incoming data from the user. In this example, we don't really need to parse any data from the request. However, in many cases, you might wanna look at the headers or the body of the request to authenticate a user or understand what the user is trying to do.

At this point, we need to implement the code to handle the request. What we wanna do is read some HTML from our file system and then send it back down to the browser. As you can see here, I'm creating a file called home dot HTML with just some generic HTML markup inside. In our source code, we can then import read file from Node's file system module. Just like we did earlier, we'll read the file, use UTF aid encoding.

Then in our callback function here, we'll have access to the HTML string, and we can send a response back down to the client by calling response dot send. And, also, if there's an error in the callback, we can handle that by sending a response with a status code of 500, which means a server error, so the user knows that something went wrong on the server. And that's all there is to it. We now have a way to send HTML from the server to the client. Now we just need to tell our Express app to start listening to incoming requests.

We do that by defining a port, which will normally come from a node environment variable. Then we call app dot listen with that port, and when it starts up, we'll console log that the app is available on local host 3,000. You You can start it up by opening the command line and running Node with the current working directory. If you go ahead and open it in the browser, you should see your HTML return back to you. Now there is one important thing you should know at this point, and that's that callbacks can be very difficult to work with, especially as your app grows in complexity.

It often leads to a state known as callback hell where you have a bunch of callbacks nested within callbacks, within more callbacks, and so on. A great way to avoid code like this is to use promises instead of callbacks, and that's very easy to do in Node. Js. Instead of importing read file from f s, We'll import it from f s dot promises. We can make our callback function async, and then we can write the response in a single line of code by saying response send and then await the operation to read file.

That's much more concise and readable, but it's especially useful when you have multiple async operations to handle in a single request. So So now that we've built a Node. Js app, how do we deploy it to the cloud so people can actually use it? There's a bunch of different ways we could do it, but an easy and free way is with Google App Engine. App Engine has what's known as a standard environment for Node.

Js up to version 12. And what that does is provide you with a server in the cloud that scales automatically based on the incoming graphic to your app. It's incredibly easy to set up, but you first need to have a Google Cloud Platform account and also the Google Cloud command line tools installed on your local system. Once you have that done, you can simply go into your source code and create an app dot YAML file. This file is used to configure your cloud server.

The only thing we need at this point is to specify a runtime of Node JS version 12. App Engine will run your code by looking in the package JSON file for a start script. So let's go in there and define a start script that runs node in the current working directory to start our Express app. From there, we can simply open the command line and run gcloud app deploy. That'll take a minute, and then it will give you a URL where you can access your app publicly on the web.

Congratulations. You're now a full stack cloud architect. If this video helped you, please like and subscribe. And if you really want to dive deep into Node. Js and Express, consider becoming a pro member at FireShip IO.

I have a whole bunch of advanced content covering real world use cases with these technologies. Thanks for watching, and I will see you in the next one.
One of the most valuable skills to have as a full stack web developer is, without a doubt, Node. Js, and that's not going to change anytime soon. But you might be wondering why I'm making a Node tutorial in 2020. Because if you've been watching the news, you may have heard that Node was killed last week by Deno. When I lay my vengeance upon me. Making the extremely chaotic JavaScript world even more convoluted. If you're just starting to learn JavaScript, this can be very confusing, and you might be wondering, should I learn Node or Deno? The answer is Node, especially if you want to get a job or build a product. When something becomes as established as Node, it doesn't get replaced. You'll see things like this happen all the time. A few years ago, everybody was saying GraphQL would replace REST. And 10 years ago, people were saying Node. Js would replace PHP. These predictions never pan out and are just built on top of hype. That being said, I do think Deno is awesome and has a bright future. Both. Both. Both. Both is good. And if you learn Node today, those skills will mostly transfer to Deno tomorrow. In today's video, you'll learn the basics of Node.
Js in 7 easy steps. Then we'll put those steps together to build a full stack web application. Because in my opinion, the best way to learn how to code is to build something meaningful from scratch. By the end of the video, we'll deploy our code to the cloud so you can show off your API to your friends and family. If you're new here, like and subscribe and head over to FireShip IO to follow along with the full write up to this video. Step 1 is to understand what Node JS is and why you would wanna use it.
streaming
video gaming
programming
Node. Js itself is not a programming language, but rather a runtime that allows you to run JavaScript on a server. You see, when JavaScript first came around in the 19 nineties, It was designed as a simple scripting language to run-in the browser. As the web platform evolved, JavaScript became more and more powerful. And in 2009, we saw the initial release of Node. Js. Now up until this time, it was impossible to write JavaScript code on the server. Most servers were written in languages like Java or PHP. It revolutionized web development because now a web developer could write a full stack application in a single language. So that's all Node. Js is. It's a way for developers to write JavaScript on the server when it could previously only be written in a web browser. Now there are a lot of low level implementation details here that you don't need to know about as a beginner. But if you really wanna dive into how Node. Js works, check out my how it's made video. But what kind of problems can Node. Js actually solve? Node. Js can do a lot. So instead of listing out a bunch of use cases, I'll show you how to build a full stack web application from scratch. This is how it'll work. You visit a URL on the Internet that points to your server. When the request is received, we can use Node to handle the request and read a file from the server's file system and then respond back to the client so they can view the HTML in the browser. Step 2, how to install Node. Js. Node can be installed on Windows, Mac, or Linux and is probably already installed on your system. In this tutorial, I'll be using Ubuntu via the Windows subsystem for Linux. At this point, I've just opened up an empty directory in Versus Code and then pulled up a terminal session. You can find out if Node is installed on your system by running node flag v from the command line. As you can see here, I'm running version 12 dot 16, which is the current long term support version. If If you get an error at this point, it means that you don't have Node installed. And even if you do have Node installed, it'd be a very good idea to use a package called Node Version manager to manage the installation of different node versions on your system. There's an n v m package for Mac and Linux or a separate package for Windows. Go ahead and follow the instructions to install NVM on your system, and that will give you the ability to install any version of Node. Js that you want. For example, you could run nvm install 12 dot 16 dot 3, then run nvm use to use that specific version as your runtime. And now that we have Node JS installed, we can move on to step 3, hello world. But first, if you're serious about Node. Js, consider becoming a pro member at FireShip IO. It'll give you access to my full length courses, almost all of which cover advanced use cases for Node. Js, but we need to walk before we can run.
So how do we actually use this thing? One way to mess around with Node is in Repel mode, which stands for read eval print loop. When you type node into the command line, it allows you to run JavaScript code and will print out the results. For example, we can type console log hello world, and it will log that value out. This is nice for messing around, but go ahead and hit control c twice to shut it down.
programming
Because in most cases, we'll want to execute JavaScript code that lives in an actual file. The default entry point into a Node. Js app is the index dot JS file. Go ahead and create an index JS file manually in Versus Code.
Then inside that file, add console log hello world. You can now run the code inside this file with the node command followed by the path to this file. And because it's an index file, you can actually just point to the parent directory. Congratulations. You just built a node app.
programming
Step 4, understand the Node runtime. In most ways, JavaScript works the same way it does in the browser as it does Node. Js, but there are some very important differences you should know about. First, node has a handful of built in identifiers. One of those globals is console, which we've already been using to log out values to the terminal. There's another global with a name of global. This object is a namespace that is available throughout the entire node process. For example, if we assigned a lucky number property to global, We could then access it anywhere else in our code.
If you're a front end web developer, you can compare global to the window object in the browser. Now I think the most important global to be familiar with is process, which gives you access to the currently running node process. You might use it to check the current platform or operating system, which in my case is Linux, or grab an environment variable from your server.
infrastructure
But this gives us a great opportunity to segue into step 5, how do events work in Node. Js? You'll often hear people describe it as an asynchronous event driven JavaScript runtime. The runtime implements a thing called an event loop, just like a web browser does, and it allows Node to push intensive operations off to a separate thread So only very fast non blocking operations happen on the main thread. And this is why people often call Node non blocking. It's a design choice that makes Node. Js very suitable for things that require high throughput like real time applications, web servers, and things like that. Okay. So how does that affect me as a coder? Well, normally, you won't have to think about the low level details. You just need to know how events and callbacks work.
In most cases, you'll listen to events. Events can come in many forms, but one example is on the process global that we looked at earlier. Before a node process finishes, it emits an event named exit. We can listen to this event using on and then register a callback function as the second argument.
operations
When the exit event occurs, Node. Js will run this function, and that's where it gets the name callback because the function isn't called the first time Node JS sees it. It's only called after the exit event occurs at some point in the future. This event is built into Node, but you can also create your own from scratch. We'll import an event emitter from the events module that is built into Node, and we'll look at modules in more detail in just a minute. We can create a custom event emitter by instantiating the class and then we'll register a callback that fires on the lunch event. Now that the callback is registered, You can simply call event emitter emit with that event name, and that triggers your callback function to run.
As you can see here, we emit the event twice, which will run the callback function twice. This event driven style of programming is extremely useful when you have something that is CPU intensive. And with that, we can move on to step 6, the file system. Node has a built in file system module called FS.
programming
It can read, write, and delete files on the file system, among other things, and it can also do things in a blocking or non blocking way.
Allow me to explain that by showing you an example. 1st, create a file on the file system called hello dot txt, and then add whatever text you want inside of it. In our JavaScript code, we'll import 2 functions from the file system module called read file and read file sync.
programming
Anytime you see a function that ends in sync, think blocking. Or in other words, it will need to finish all of its work before any of your other code can run. We can read a text file in node by simply passing the path to that file, and then we'll specify the encoding as utf8. Now reading a file might take a while, especially if it's a very large file. And what you'll notice here when we run our code is that the second console log won't run until after that file has been read. Luckily, you can make your code non blocking by refactoring this to a callback. With read file, we pass the same first two arguments and then add a callback function as the 3rd argument. Inside the function, we can access an error object if the operation fails or when successful, the actual text from the file. The super cool thing about this is that even though the console log to the text file comes first in our script, it's not the thing that gets executed first. Node registers the callback, executes the rest of the script, and then finally runs the callback when the file has been read. So that gives us 2 different ways to read a file, But there's actually one other way we could go about this, and that's using a Promise based solution. Promises are also asynchronous and non blocking, and they tend to produce much cleaner code when compared to callbacks. Notice how in this example, we're importing ReadFile from the Promises namespace. This gives us a function that returns a Promise when called. If you're using the latest bleeding edge version of node, you could potentially use this function with top level await, which means you can use await to resolve the promise here at the top of the script. However, since we're currently using Node version 12, we'll go ahead and wrap this in an async function. And this async await syntax will make your code much easier to read, especially when you have multiple async calls in the same function. Okay. So now that you know how to read files, I want to backtrack a little bit and talk about this require function that you see me use in the past couple examples. Step 7, modules. A module is nothing more than a JavaScript file that exports its code. Node has a bunch of built in modules like f s and events that we've already looked at, and there's a long list of other modules beyond that. The traditional way to import a module in Node is to use this require function. But a quick side note on that first because Node recently added support for ES modules, which use the import export syntax. Most Node. Js code out there written in vanilla JavaScript still uses require, so it's very important for you to know as a Node. Js developer. And if all that's confusing to you, Well, all I can say is welcome to the JavaScript world. Let's look at how we can use modules in our own codebase.
Create a new file to serve as your module, Then go into your index JS file, create a variable for the module, and then import it using require. When you console log it, you'll notice that it's currently just an empty object. In order to make a module useful, you need to export some code from it.
programming
In the module's file, you can reference this object with module dot exports. You can add new properties to the object or redefine it as a new object.
In either case, whatever you add here will now be available to use in the other file. And as you can see, the object is no longer empty when we console log it. But at some point, you'll very likely want to use somebody else's code out there in the world.
programming
And the primary place to do that is via Node Package Manager or NPM, which was recently acquired by GitHub, which itself was recently acquired by Microsoft. When you installed Node earlier in the video, it also installed npm, which is a tool you can use to install remote packages to use in your own code. The first step we'll want to take is to open the command line and run npm init. We'll use the y flag to use the default options, and what you'll notice is that it creates a package JSON file here in the root of the application.
This file can contain metadata about your project, but most importantly, it keeps track of the dependencies that you use here. At this point, we haven't installed anything yet. So let's go ahead and change that by opening the command line again and running npm install express. Express is a minimal web application framework and one of the most popular third party node modules.
software
software development
After running the command, you'll notice a few things happened here. In the package JSON file, it added express to the dependencies and pegged it to a specific version. This dependencies object allows you to manage multiple dependencies in a project and then reinstall them all at once on a different system. Now the actual raw source code for the dependency lives in this node modules directory. You should never have to modify code in the node modules directory. If you find yourself writing code in here, you're probably doing it wrong. That's because the package JSON controls how this directory is built. It fetches your remote packages, Saves the source code here and that process should be able to be repeated on any system.
Now that we have this package installed, we can import it by name in our JavaScript code. In this case, we simply require Express. And now we've reached the point where we're ready to put these 7 steps together to build a real full stack application and deploy it to the cloud.
software
What we're building here is actually just your typical full stack web application. Our server will live on a URL, and when a user makes a request to this URL in the browser, The server will respond with some HTML. In our code, we'll first create an instance of an Express app. An Express app allows us to Create different URLs and endpoints that a user could navigate to in the browser. And then we define code for the server to handle those requests. Now I don't wanna get too deep into HTTP, but when the user navigates to a URL in the browser, it's what's known as a get request, which means they're requesting some data on the server and not trying to modify or update anything on the server. With Express, we can set up an endpoint like that by calling app dot get. And then the first argument is the URL that the user will navigate to. In this case, we'll just use a forward slash for the root URL. But feel free to create multiple pages for your web app by creating different URLs. The second argument here is our callback function. You can think of every request to this URL as an event, and then you handle that event with this function. And Express gives us 2 parameters to make use of, the request and the response. The request is the incoming data from the user. In this example, we don't really need to parse any data from the request. However, in many cases, you might wanna look at the headers or the body of the request to authenticate a user or understand what the user is trying to do. At this point, we need to implement the code to handle the request. What we wanna do is read some HTML from our file system and then send it back down to the browser. As you can see here, I'm creating a file called home dot HTML with just some generic HTML markup inside. In our source code, we can then import read file from Node's file system module. Just like we did earlier, we'll read the file, use UTF aid encoding. Then in our callback function here, we'll have access to the HTML string, and we can send a response back down to the client by calling response dot send. And, also, if there's an error in the callback, we can handle that by sending a response with a status code of 500, which means a server error, so the user knows that something went wrong on the server. And that's all there is to it. We now have a way to send HTML from the server to the client. Now we just need to tell our Express app to start listening to incoming requests. We do that by defining a port, which will normally come from a node environment variable. Then we call app dot listen with that port, and when it starts up, we'll console log that the app is available on local host 3,000. You You can start it up by opening the command line and running Node with the current working directory. If you go ahead and open it in the browser, you should see your HTML return back to you. Now there is one important thing you should know at this point, and that's that callbacks can be very difficult to work with, especially as your app grows in complexity. It often leads to a state known as callback hell where you have a bunch of callbacks nested within callbacks, within more callbacks, and so on. A great way to avoid code like this is to use promises instead of callbacks, and that's very easy to do in Node. Js. Instead of importing read file from f s, We'll import it from f s dot promises.
We can make our callback function async, and then we can write the response in a single line of code by saying response send and then await the operation to read file. That's much more concise and readable, but it's especially useful when you have multiple async operations to handle in a single request.
programming
So So now that we've built a Node. Js app, how do we deploy it to the cloud so people can actually use it? There's a bunch of different ways we could do it, but an easy and free way is with Google App Engine.
App Engine has what's known as a standard environment for Node. Js up to version 12. And what that does is provide you with a server in the cloud that scales automatically based on the incoming graphic to your app. It's incredibly easy to set up, but you first need to have a Google Cloud Platform account and also the Google Cloud command line tools installed on your local system. Once you have that done, you can simply go into your source code and create an app dot YAML file.
google
software
This file is used to configure your cloud server. The only thing we need at this point is to specify a runtime of Node JS version 12. App Engine will run your code by looking in the package JSON file for a start script. So let's go in there and define a start script that runs node in the current working directory to start our Express app. From there, we can simply open the command line and run gcloud app deploy. That'll take a minute, and then it will give you a URL where you can access your app publicly on the web. Congratulations. You're now a full stack cloud architect.
If this video helped you, please like and subscribe. And if you really want to dive deep into Node. Js and Express, consider becoming a pro member at FireShip IO. I have a whole bunch of advanced content covering real world use cases with these technologies. Thanks for watching, and I will see you in the next one.
technology
multimedia
websites
products
software
streaming
One of the most valuable skills to have as a full stack web developer is, without a doubt, Node. Js, and that's not going to change anytime soon. But you might be wondering why I'm making a Node tutorial in 2020. Because if you've been watching the news, you may have heard that Node was killed last week by Deno. When I lay my vengeance upon me. Making the extremely chaotic JavaScript world even more convoluted. If you're just starting to learn JavaScript, this can be very confusing, and you might be wondering, should I learn Node or Deno? The answer is Node, especially if you want to get a job or build a product. When something becomes as established as Node, it doesn't get replaced. You'll see things like this happen all the time. A few years ago, everybody was saying GraphQL would replace REST. And 10 years ago, people were saying Node. Js would replace PHP. These predictions never pan out and are just built on top of hype. That being said, I do think Deno is awesome and has a bright future. Both. Both. Both. Both is good. And if you learn Node today, those skills will mostly transfer to Deno tomorrow. In today's video, you'll learn the basics of Node. Js in 7 easy steps. Then we'll put those steps together to build a full stack web application. Because in my opinion, the best way to learn how to code is to build something meaningful from scratch. By the end of the video, we'll deploy our code to the cloud so you can show off your API to your friends and family. If you're new here, like and subscribe and head over to FireShip IO to follow along with the full write up to this video. Step 1 is to understand what Node JS is and why you would wanna use it. Node. Js itself is not a programming language, but rather a runtime that allows you to run JavaScript on a server. You see, when JavaScript first came around in the 19 nineties, It was designed as a simple scripting language to run-in the browser. As the web platform evolved, JavaScript became more and more powerful. And in 2009, we saw the initial release of Node. Js. Now up until this time, it was impossible to write JavaScript code on the server. Most servers were written in languages like Java or PHP. It revolutionized web development because now a web developer could write a full stack application in a single language. So that's all Node. Js is. It's a way for developers to write JavaScript on the server when it could previously only be written in a web browser. Now there are a lot of low level implementation details here that you don't need to know about as a beginner. But if you really wanna dive into how Node. Js works, check out my how it's made video. But what kind of problems can Node. Js actually solve? Node. Js can do a lot. So instead of listing out a bunch of use cases, I'll show you how to build a full stack web application from scratch. This is how it'll work. You visit a URL on the Internet that points to your server. When the request is received, we can use Node to handle the request and read a file from the server's file system and then respond back to the client so they can view the HTML in the browser. Step 2, how to install Node. Js. Node can be installed on Windows, Mac, or Linux and is probably already installed on your system. In this tutorial, I'll be using Ubuntu via the Windows subsystem for Linux. At this point, I've just opened up an empty directory in Versus Code and then pulled up a terminal session. You can find out if Node is installed on your system by running node flag v from the command line. As you can see here, I'm running version 12 dot 16, which is the current long term support version. If If you get an error at this point, it means that you don't have Node installed. And even if you do have Node installed, it'd be a very good idea to use a package called Node Version manager to manage the installation of different node versions on your system. There's an n v m package for Mac and Linux or a separate package for Windows. Go ahead and follow the instructions to install NVM on your system, and that will give you the ability to install any version of Node. Js that you want. For example, you could run nvm install 12 dot 16 dot 3, then run nvm use to use that specific version as your runtime. And now that we have Node JS installed, we can move on to step 3, hello world. But first, if you're serious about Node. Js, consider becoming a pro member at FireShip IO. It'll give you access to my full length courses, almost all of which cover advanced use cases for Node. Js, but we need to walk before we can run. So how do we actually use this thing? One way to mess around with Node is in Repel mode, which stands for read eval print loop. When you type node into the command line, it allows you to run JavaScript code and will print out the results. For example, we can type console log hello world, and it will log that value out. This is nice for messing around, but go ahead and hit control c twice to shut it down. Because in most cases, we'll want to execute JavaScript code that lives in an actual file. The default entry point into a Node. Js app is the index dot JS file. Go ahead and create an index JS file manually in Versus Code. Then inside that file, add console log hello world. You can now run the code inside this file with the node command followed by the path to this file. And because it's an index file, you can actually just point to the parent directory. Congratulations. You just built a node app. Step 4, understand the Node runtime. In most ways, JavaScript works the same way it does in the browser as it does Node. Js, but there are some very important differences you should know about. First, node has a handful of built in identifiers. One of those globals is console, which we've already been using to log out values to the terminal. There's another global with a name of global. This object is a namespace that is available throughout the entire node process. For example, if we assigned a lucky number property to global, We could then access it anywhere else in our code. If you're a front end web developer, you can compare global to the window object in the browser. Now I think the most important global to be familiar with is process, which gives you access to the currently running node process. You might use it to check the current platform or operating system, which in my case is Linux, or grab an environment variable from your server. But this gives us a great opportunity to segue into step 5, how do events work in Node. Js? You'll often hear people describe it as an asynchronous event driven JavaScript runtime. The runtime implements a thing called an event loop, just like a web browser does, and it allows Node to push intensive operations off to a separate thread So only very fast non blocking operations happen on the main thread. And this is why people often call Node non blocking. It's a design choice that makes Node. Js very suitable for things that require high throughput like real time applications, web servers, and things like that. Okay. So how does that affect me as a coder? Well, normally, you won't have to think about the low level details. You just need to know how events and callbacks work. In most cases, you'll listen to events. Events can come in many forms, but one example is on the process global that we looked at earlier. Before a node process finishes, it emits an event named exit. We can listen to this event using on and then register a callback function as the second argument. When the exit event occurs, Node. Js will run this function, and that's where it gets the name callback because the function isn't called the first time Node JS sees it. It's only called after the exit event occurs at some point in the future. This event is built into Node, but you can also create your own from scratch. We'll import an event emitter from the events module that is built into Node, and we'll look at modules in more detail in just a minute. We can create a custom event emitter by instantiating the class and then we'll register a callback that fires on the lunch event. Now that the callback is registered, You can simply call event emitter emit with that event name, and that triggers your callback function to run. As you can see here, we emit the event twice, which will run the callback function twice. This event driven style of programming is extremely useful when you have something that is CPU intensive. And with that, we can move on to step 6, the file system. Node has a built in file system module called FS. It can read, write, and delete files on the file system, among other things, and it can also do things in a blocking or non blocking way. Allow me to explain that by showing you an example. 1st, create a file on the file system called hello dot txt, and then add whatever text you want inside of it. In our JavaScript code, we'll import 2 functions from the file system module called read file and read file sync. Anytime you see a function that ends in sync, think blocking. Or in other words, it will need to finish all of its work before any of your other code can run. We can read a text file in node by simply passing the path to that file, and then we'll specify the encoding as utf8. Now reading a file might take a while, especially if it's a very large file. And what you'll notice here when we run our code is that the second console log won't run until after that file has been read. Luckily, you can make your code non blocking by refactoring this to a callback. With read file, we pass the same first two arguments and then add a callback function as the 3rd argument. Inside the function, we can access an error object if the operation fails or when successful, the actual text from the file. The super cool thing about this is that even though the console log to the text file comes first in our script, it's not the thing that gets executed first. Node registers the callback, executes the rest of the script, and then finally runs the callback when the file has been read. So that gives us 2 different ways to read a file, But there's actually one other way we could go about this, and that's using a Promise based solution. Promises are also asynchronous and non blocking, and they tend to produce much cleaner code when compared to callbacks. Notice how in this example, we're importing ReadFile from the Promises namespace. This gives us a function that returns a Promise when called. If you're using the latest bleeding edge version of node, you could potentially use this function with top level await, which means you can use await to resolve the promise here at the top of the script. However, since we're currently using Node version 12, we'll go ahead and wrap this in an async function. And this async await syntax will make your code much easier to read, especially when you have multiple async calls in the same function. Okay. So now that you know how to read files, I want to backtrack a little bit and talk about this require function that you see me use in the past couple examples. Step 7, modules. A module is nothing more than a JavaScript file that exports its code. Node has a bunch of built in modules like f s and events that we've already looked at, and there's a long list of other modules beyond that. The traditional way to import a module in Node is to use this require function. But a quick side note on that first because Node recently added support for ES modules, which use the import export syntax. Most Node. Js code out there written in vanilla JavaScript still uses require, so it's very important for you to know as a Node. Js developer. And if all that's confusing to you, Well, all I can say is welcome to the JavaScript world. Let's look at how we can use modules in our own codebase. Create a new file to serve as your module, Then go into your index JS file, create a variable for the module, and then import it using require. When you console log it, you'll notice that it's currently just an empty object. In order to make a module useful, you need to export some code from it. In the module's file, you can reference this object with module dot exports. You can add new properties to the object or redefine it as a new object. In either case, whatever you add here will now be available to use in the other file. And as you can see, the object is no longer empty when we console log it. But at some point, you'll very likely want to use somebody else's code out there in the world. And the primary place to do that is via Node Package Manager or NPM, which was recently acquired by GitHub, which itself was recently acquired by Microsoft. When you installed Node earlier in the video, it also installed npm, which is a tool you can use to install remote packages to use in your own code. The first step we'll want to take is to open the command line and run npm init. We'll use the y flag to use the default options, and what you'll notice is that it creates a package JSON file here in the root of the application. This file can contain metadata about your project, but most importantly, it keeps track of the dependencies that you use here. At this point, we haven't installed anything yet. So let's go ahead and change that by opening the command line again and running npm install express. Express is a minimal web application framework and one of the most popular third party node modules. After running the command, you'll notice a few things happened here. In the package JSON file, it added express to the dependencies and pegged it to a specific version. This dependencies object allows you to manage multiple dependencies in a project and then reinstall them all at once on a different system. Now the actual raw source code for the dependency lives in this node modules directory. You should never have to modify code in the node modules directory. If you find yourself writing code in here, you're probably doing it wrong. That's because the package JSON controls how this directory is built. It fetches your remote packages, Saves the source code here and that process should be able to be repeated on any system. Now that we have this package installed, we can import it by name in our JavaScript code. In this case, we simply require Express. And now we've reached the point where we're ready to put these 7 steps together to build a real full stack application and deploy it to the cloud. What we're building here is actually just your typical full stack web application. Our server will live on a URL, and when a user makes a request to this URL in the browser, The server will respond with some HTML. In our code, we'll first create an instance of an Express app. An Express app allows us to Create different URLs and endpoints that a user could navigate to in the browser. And then we define code for the server to handle those requests. Now I don't wanna get too deep into HTTP, but when the user navigates to a URL in the browser, it's what's known as a get request, which means they're requesting some data on the server and not trying to modify or update anything on the server. With Express, we can set up an endpoint like that by calling app dot get. And then the first argument is the URL that the user will navigate to. In this case, we'll just use a forward slash for the root URL. But feel free to create multiple pages for your web app by creating different URLs. The second argument here is our callback function. You can think of every request to this URL as an event, and then you handle that event with this function. And Express gives us 2 parameters to make use of, the request and the response. The request is the incoming data from the user. In this example, we don't really need to parse any data from the request. However, in many cases, you might wanna look at the headers or the body of the request to authenticate a user or understand what the user is trying to do. At this point, we need to implement the code to handle the request. What we wanna do is read some HTML from our file system and then send it back down to the browser. As you can see here, I'm creating a file called home dot HTML with just some generic HTML markup inside. In our source code, we can then import read file from Node's file system module. Just like we did earlier, we'll read the file, use UTF aid encoding. Then in our callback function here, we'll have access to the HTML string, and we can send a response back down to the client by calling response dot send. And, also, if there's an error in the callback, we can handle that by sending a response with a status code of 500, which means a server error, so the user knows that something went wrong on the server. And that's all there is to it. We now have a way to send HTML from the server to the client. Now we just need to tell our Express app to start listening to incoming requests. We do that by defining a port, which will normally come from a node environment variable. Then we call app dot listen with that port, and when it starts up, we'll console log that the app is available on local host 3,000. You You can start it up by opening the command line and running Node with the current working directory. If you go ahead and open it in the browser, you should see your HTML return back to you. Now there is one important thing you should know at this point, and that's that callbacks can be very difficult to work with, especially as your app grows in complexity. It often leads to a state known as callback hell where you have a bunch of callbacks nested within callbacks, within more callbacks, and so on. A great way to avoid code like this is to use promises instead of callbacks, and that's very easy to do in Node. Js. Instead of importing read file from f s, We'll import it from f s dot promises. We can make our callback function async, and then we can write the response in a single line of code by saying response send and then await the operation to read file. That's much more concise and readable, but it's especially useful when you have multiple async operations to handle in a single request. So So now that we've built a Node. Js app, how do we deploy it to the cloud so people can actually use it? There's a bunch of different ways we could do it, but an easy and free way is with Google App Engine. App Engine has what's known as a standard environment for Node. Js up to version 12. And what that does is provide you with a server in the cloud that scales automatically based on the incoming graphic to your app. It's incredibly easy to set up, but you first need to have a Google Cloud Platform account and also the Google Cloud command line tools installed on your local system. Once you have that done, you can simply go into your source code and create an app dot YAML file. This file is used to configure your cloud server. The only thing we need at this point is to specify a runtime of Node JS version 12. App Engine will run your code by looking in the package JSON file for a start script. So let's go in there and define a start script that runs node in the current working directory to start our Express app. From there, we can simply open the command line and run gcloud app deploy. That'll take a minute, and then it will give you a URL where you can access your app publicly on the web. Congratulations. You're now a full stack cloud architect. If this video helped you, please like and subscribe. And if you really want to dive deep into Node. Js and Express, consider becoming a pro member at FireShip IO. I have a whole bunch of advanced content covering real world use cases with these technologies. Thanks for watching, and I will see you in the next one.
One
one
of
of
the
the
most
most
valuable
valuable
skills
skills
to
to
have
have
as
as
a
a
full
full
stack
stack
web
web
developer
developer
is,
is
without
without
a
a
doubt,
doubt
Node.
node
Js,
js
and
and
that's
that's
not
not
going
going
to
to
change
change
anytime
anytime
soon.
soon
But
but
you
you
might
might
be
be
wondering
wondering
why
why
I'm
i'm
making
making
a
a
Node
node
tutorial
tutorial
in
in
2020.
2020
Because
because
if
if
you've
you've
been
been
watching
watching
the
the
news,
news
you
you
may
may
have
have
heard
heard
that
that
Node
node
was
was
killed
killed
last
last
week
week
by
by
Deno.
deno
When
when
I
i
lay
lay
my
my
vengeance
vengeance
upon
upon
me.
me
Making
making
the
the
extremely
extremely
chaotic
chaotic
JavaScript
javascript
world
world
even
even
more
more
convoluted.
convoluted
If
if
you're
you're
just
just
starting
starting
to
to
learn
learn
JavaScript,
javascript
this
this
can
can
be
be
very
very
confusing,
confusing
and
and
you
you
might
might
be
be
wondering,
wondering
should
should
I
i
learn
learn
Node
node
or
or
Deno?
deno
The
the
answer
answer
is
is
Node,
node
especially
especially
if
if
you
you
want
want
to
to
get
get
a
a
job
job
or
or
build
build
a
a
product.
product
When
when
something
something
becomes
becomes
as
as
established
established
as
as
Node,
node
it
it
doesn't
doesn't
get
get
replaced.
replaced
You'll
you'll
see
see
things
things
like
like
this
this
happen
happen
all
all
the
the
time.
time
A
a
few
few
years
years
ago,
ago
everybody
everybody
was
was
saying
saying
GraphQL
graphql
would
would
replace
replace
REST.
rest
And
and
10
10
years
years
ago,
ago
people
people
were
were
saying
saying
Node.
node
Js
js
would
would
replace
replace
PHP.
php
These
these
predictions
predictions
never
never
pan
pan
out
out
and
and
are
are
just
just
built
built
on
on
top
top
of
of
hype.
hype
That
that
being
being
said,
said
I
i
do
do
think
think
Deno
deno
is
is
awesome
awesome
and
and
has
has
a
a
bright
bright
future.
future
Both.
both
Both.
both
Both.
both
Both
both
is
is
good.
good
And
and
if
if
you
you
learn
learn
Node
node
today,
today
those
those
skills
skills
will
will
mostly
mostly
transfer
transfer
to
to
Deno
deno
tomorrow.
tomorrow
In
in
today's
today's
video,
video
you'll
you'll
learn
learn
the
the
basics
basics
of
of
Node.
node
Js
js
in
in
7
7
easy
easy
steps.
steps
Then
then
we'll
we'll
put
put
those
those
steps
steps
together
together
to
to
build
build
a
a
full
full
stack
stack
web
web
application.
application
Because
because
in
in
my
my
opinion,
opinion
the
the
best
best
way
way
to
to
learn
learn
how
how
to
to
code
code
is
is
to
to
build
build
something
something
meaningful
meaningful
from
from
scratch.
scratch
By
by
the
the
end
end
of
of
the
the
video,
video
we'll
we'll
deploy
deploy
our
our
code
code
to
to
the
the
cloud
cloud
so
so
you
you
can
can
show
show
off
off
your
your
API
api
to
to
your
your
friends
friends
and
and
family.
family
If
if
you're
you're
new
new
here,
here
like
like
and
and
subscribe
subscribe
and
and
head
head
over
over
to
to
FireShip
fireship
IO
io
to
to
follow
follow
along
along
with
with
the
the
full
full
write
write
up
up
to
to
this
this
video.
video
Step
step
1
1
is
is
to
to
understand
understand
what
what
Node
node
JS
js
is
is
and
and
why
why
you
you
would
would
wanna
wanna
use
use
it.
it
Node.
node
Js
js
itself
itself
is
is
not
not
a
a
programming
programming
language,
language
but
but
rather
rather
a
a
runtime
runtime
that
that
allows
allows
you
you
to
to
run
run
JavaScript
javascript
on
on
a
a
server.
server
You
you
see,
see
when
when
JavaScript
javascript
first
first
came
came
around
around
in
in
the
the
19
19
nineties,
nineties
It
it
was
was
designed
designed
as
as
a
a
simple
simple
scripting
scripting
language
language
to
to
run-in
run-in
the
the
browser.
browser
As
as
the
the
web
web
platform
platform
evolved,
evolved
JavaScript
javascript
became
became
more
more
and
and
more
more
powerful.
powerful
And
and
in
in
2009,
2009
we
we
saw
saw
the
the
initial
initial
release
release
of
of
Node.
node
Js.
js
Now
now
up
up
until
until
this
this
time,
time
it
it
was
was
impossible
impossible
to
to
write
write
JavaScript
javascript
code
code
on
on
the
the
server.
server
Most
most
servers
servers
were
were
written
written
in
in
languages
languages
like
like
Java
java
or
or
PHP.
php
It
it
revolutionized
revolutionized
web
web
development
development
because
because
now
now
a
a
web
web
developer
developer
could
could
write
write
a
a
full
full
stack
stack
application
application
in
in
a
a
single
single
language.
language
So
so
that's
that's
all
all
Node.
node
Js
js
is.
is
It's
it's
a
a
way
way
for
for
developers
developers
to
to
write
write
JavaScript
javascript
on
on
the
the
server
server
when
when
it
it
could
could
previously
previously
only
only
be
be
written
written
in
in
a
a
web
web
browser.
browser
Now
now
there
there
are
are
a
a
lot
lot
of
of
low
low
level
level
implementation
implementation
details
details
here
here
that
that
you
you
don't
don't
need
need
to
to
know
know
about
about
as
as
a
a
beginner.
beginner
But
but
if
if
you
you
really
really
wanna
wanna
dive
dive
into
into
how
how
Node.
node
Js
js
works,
works
check
check
out
out
my
my
how
how
it's
it's
made
made
video.
video
But
but
what
what
kind
kind
of
of
problems
problems
can
can
Node.
node
Js
js
actually
actually
solve?
solve
Node.
node
Js
js
can
can
do
do
a
a
lot.
lot
So
so
instead
instead
of
of
listing
listing
out
out
a
a
bunch
bunch
of
of
use
use
cases,
cases
I'll
i'll
show
show
you
you
how
how
to
to
build
build
a
a
full
full
stack
stack
web
web
application
application
from
from
scratch.
scratch
This
this
is
is
how
how
it'll
it'll
work.
work
You
you
visit
visit
a
a
URL
url
on
on
the
the
Internet
internet
that
that
points
points
to
to
your
your
server.
server
When
when
the
the
request
request
is
is
received,
received
we
we
can
can
use
use
Node
node
to
to
handle
handle
the
the
request
request
and
and
read
read
a
a
file
file
from
from
the
the
server's
server's
file
file
system
system
and
and
then
then
respond
respond
back
back
to
to
the
the
client
client
so
so
they
they
can
can
view
view
the
the
HTML
html
in
in
the
the
browser.
browser
Step
step
2,
2
how
how
to
to
install
install
Node.
node
Js.
js
Node
node
can
can
be
be
installed
installed
on
on
Windows,
windows
Mac,
mac
or
or
Linux
linux
and
and
is
is
probably
probably
already
already
installed
installed
on
on
your
your
system.
system
In
in
this
this
tutorial,
tutorial
I'll
i'll
be
be
using
using
Ubuntu
ubuntu
via
via
the
the
Windows
windows
subsystem
subsystem
for
for
Linux.
linux
At
at
this
this
point,
point
I've
i've
just
just
opened
opened
up
up
an
an
empty
empty
directory
directory
in
in
Versus
versus
Code
code
and
and
then
then
pulled
pulled
up
up
a
a
terminal
terminal
session.
session
You
you
can
can
find
find
out
out
if
if
Node
node
is
is
installed
installed
on
on
your
your
system
system
by
by
running
running
node
node
flag
flag
v
v
from
from
the
the
command
command
line.
line
As
as
you
you
can
can
see
see
here,
here
I'm
i'm
running
running
version
version
12
12
dot
dot
16,
16
which
which
is
is
the
the
current
current
long
long
term
term
support
support
version.
version
If
if
If
if
you
you
get
get
an
an
error
error
at
at
this
this
point,
point
it
it
means
means
that
that
you
you
don't
don't
have
have
Node
node
installed.
installed
And
and
even
even
if
if
you
you
do
do
have
have
Node
node
installed,
installed
it'd
it'd
be
be
a
a
very
very
good
good
idea
idea
to
to
use
use
a
a
package
package
called
called
Node
node
Version
version
manager
manager
to
to
manage
manage
the
the
installation
installation
of
of
different
different
node
node
versions
versions
on
on
your
your
system.
system
There's
there's
an
an
n
n
v
v
m
m
package
package
for
for
Mac
mac
and
and
Linux
linux
or
or
a
a
separate
separate
package
package
for
for
Windows.
windows
Go
go
ahead
ahead
and
and
follow
follow
the
the
instructions
instructions
to
to
install
install
NVM
nvm
on
on
your
your
system,
system
and
and
that
that
will
will
give
give
you
you
the
the
ability
ability
to
to
install
install
any
any
version
version
of
of
Node.
node
Js
js
that
that
you
you
want.
want
For
for
example,
example
you
you
could
could
run
run
nvm
nvm
install
install
12
12
dot
dot
16
16
dot
dot
3,
3
then
then
run
run
nvm
nvm
use
use
to
to
use
use
that
that
specific
specific
version
version
as
as
your
your
runtime.
runtime
And
and
now
now
that
that
we
we
have
have
Node
node
JS
js
installed,
installed
we
we
can
can
move
move
on
on
to
to
step
step
3,
3
hello
hello
world.
world
But
but
first,
first
if
if
you're
you're
serious
serious
about
about
Node.
node
Js,
js
consider
consider
becoming
becoming
a
a
pro
pro
member
member
at
at
FireShip
fireship
IO.
io
It'll
it'll
give
give
you
you
access
access
to
to
my
my
full
full
length
length
courses,
courses
almost
almost
all
all
of
of
which
which
cover
cover
advanced
advanced
use
use
cases
cases
for
for
Node.
node
Js,
js
but
but
we
we
need
need
to
to
walk
walk
before
before
we
we
can
can
run.
run
So
so
how
how
do
do
we
we
actually
actually
use
use
this
this
thing?
thing
One
one
way
way
to
to
mess
mess
around
around
with
with
Node
node
is
is
in
in
Repel
repel
mode,
mode
which
which
stands
stands
for
for
read
read
eval
eval
print
print
loop.
loop
When
when
you
you
type
type
node
node
into
into
the
the
command
command
line,
line
it
it
allows
allows
you
you
to
to
run
run
JavaScript
javascript
code
code
and
and
will
will
print
print
out
out
the
the
results.
results
For
for
example,
example
we
we
can
can
type
type
console
console
log
log
hello
hello
world,
world
and
and
it
it
will
will
log
log
that
that
value
value
out.
out
This
this
is
is
nice
nice
for
for
messing
messing
around,
around
but
but
go
go
ahead
ahead
and
and
hit
hit
control
control
c
c
twice
twice
to
to
shut
shut
it
it
down.
down
Because
because
in
in
most
most
cases,
cases
we'll
we'll
want
want
to
to
execute
execute
JavaScript
javascript
code
code
that
that
lives
lives
in
in
an
an
actual
actual
file.
file
The
the
default
default
entry
entry
point
point
into
into
a
a
Node.
node
Js
js
app
app
is
is
the
the
index
index
dot
dot
JS
js
file.
file
Go
go
ahead
ahead
and
and
create
create
an
an
index
index
JS
js
file
file
manually
manually
in
in
Versus
versus
Code.
code
Then
then
inside
inside
that
that
file,
file
add
add
console
console
log
log
hello
hello
world.
world
You
you
can
can
now
now
run
run
the
the
code
code
inside
inside
this
this
file
file
with
with
the
the
node
node
command
command
followed
followed
by
by
the
the
path
path
to
to
this
this
file.
file
And
and
because
because
it's
it's
an
an
index
index
file,
file
you
you
can
can
actually
actually
just
just
point
point
to
to
the
the
parent
parent
directory.
directory
Congratulations.
congratulations
You
you
just
just
built
built
a
a
node
node
app.
app
Step
step
4,
4
understand
understand
the
the
Node
node
runtime.
runtime
In
in
most
most
ways,
ways
JavaScript
javascript
works
works
the
the
same
same
way
way
it
it
does
does
in
in
the
the
browser
browser
as
as
it
it
does
does
Node.
node
Js,
js
but
but
there
there
are
are
some
some
very
very
important
important
differences
differences
you
you
should
should
know
know
about.
about
First,
first
node
node
has
has
a
a
handful
handful
of
of
built
built
in
in
identifiers.
identifiers
One
one
of
of
those
those
globals
globals
is
is
console,
console
which
which
we've
we've
already
already
been
been
using
using
to
to
log
log
out
out
values
values
to
to
the
the
terminal.
terminal
There's
there's
another
another
global
global
with
with
a
a
name
name
of
of
global.
global
This
this
object
object
is
is
a
a
namespace
namespace
that
that
is
is
available
available
throughout
throughout
the
the
entire
entire
node
node
process.
process
For
for
example,
example
if
if
we
we
assigned
assigned
a
a
lucky
lucky
number
number
property
property
to
to
global,
global
We
we
could
could
then
then
access
access
it
it
anywhere
anywhere
else
else
in
in
our
our
code.
code
If
if
you're
you're
a
a
front
front
end
end
web
web
developer,
developer
you
you
can
can
compare
compare
global
global
to
to
the
the
window
window
object
object
in
in
the
the
browser.
browser
Now
now
I
i
think
think
the
the
most
most
important
important
global
global
to
to
be
be
familiar
familiar
with
with
is
is
process,
process
which
which
gives
gives
you
you
access
access
to
to
the
the
currently
currently
running
running
node
node
process.
process
You
you
might
might
use
use
it
it
to
to
check
check
the
the
current
current
platform
platform
or
or
operating
operating
system,
system
which
which
in
in
my
my
case
case
is
is
Linux,
linux
or
or
grab
grab
an
an
environment
environment
variable
variable
from
from
your
your
server.
server
But
but
this
this
gives
gives
us
us
a
a
great
great
opportunity
opportunity
to
to
segue
segue
into
into
step
step
5,
5
how
how
do
do
events
events
work
work
in
in
Node.
node
Js?
js
You'll
you'll
often
often
hear
hear
people
people
describe
describe
it
it
as
as
an
an
asynchronous
asynchronous
event
event
driven
driven
JavaScript
javascript
runtime.
runtime
The
the
runtime
runtime
implements
implements
a
a
thing
thing
called
called
an
an
event
event
loop,
loop
just
just
like
like
a
a
web
web
browser
browser
does,
does
and
and
it
it
allows
allows
Node
node
to
to
push
push
intensive
intensive
operations
operations
off
off
to
to
a
a
separate
separate
thread
thread
So
so
only
only
very
very
fast
fast
non
non
blocking
blocking
operations
operations
happen
happen
on
on
the
the
main
main
thread.
thread
And
and
this
this
is
is
why
why
people
people
often
often
call
call
Node
node
non
non
blocking.
blocking
It's
it's
a
a
design
design
choice
choice
that
that
makes
makes
Node.
node
Js
js
very
very
suitable
suitable
for
for
things
things
that
that
require
require
high
high
throughput
throughput
like
like
real
real
time
time
applications,
applications
web
web
servers,
servers
and
and
things
things
like
like
that.
that
Okay.
okay
So
so
how
how
does
does
that
that
affect
affect
me
me
as
as
a
a
coder?
coder
Well,
well
normally,
normally
you
you
won't
won't
have
have
to
to
think
think
about
about
the
the
low
low
level
level
details.
details
You
you
just
just
need
need
to
to
know
know
how
how
events
events
and
and
callbacks
callbacks
work.
work
In
in
most
most
cases,
cases
you'll
you'll
listen
listen
to
to
events.
events
Events
events
can
can
come
come
in
in
many
many
forms,
forms
but
but
one
one
example
example
is
is
on
on
the
the
process
process
global
global
that
that
we
we
looked
looked
at
at
earlier.
earlier
Before
before
a
a
node
node
process
process
finishes,
finishes
it
it
emits
emits
an
an
event
event
named
named
exit.
exit
We
we
can
can
listen
listen
to
to
this
this
event
event
using
using
on
on
and
and
then
then
register
register
a
a
callback
callback
function
function
as
as
the
the
second
second
argument.
argument
When
when
the
the
exit
exit
event
event
occurs,
occurs
Node.
node
Js
js
will
will
run
run
this
this
function,
function
and
and
that's
that's
where
where
it
it
gets
gets
the
the
name
name
callback
callback
because
because
the
the
function
function
isn't
isn't
called
called
the
the
first
first
time
time
Node
node
JS
js
sees
sees
it.
it
It's
it's
only
only
called
called
after
after
the
the
exit
exit
event
event
occurs
occurs
at
at
some
some
point
point
in
in
the
the
future.
future
This
this
event
event
is
is
built
built
into
into
Node,
node
but
but
you
you
can
can
also
also
create
create
your
your
own
own
from
from
scratch.
scratch
We'll
we'll
import
import
an
an
event
event
emitter
emitter
from
from
the
the
events
events
module
module
that
that
is
is
built
built
into
into
Node,
node
and
and
we'll
we'll
look
look
at
at
modules
modules
in
in
more
more
detail
detail
in
in
just
just
a
a
minute.
minute
We
we
can
can
create
create
a
a
custom
custom
event
event
emitter
emitter
by
by
instantiating
instantiating
the
the
class
class
and
and
then
then
we'll
we'll
register
register
a
a
callback
callback
that
that
fires
fires
on
on
the
the
lunch
lunch
event.
event
Now
now
that
that
the
the
callback
callback
is
is
registered,
registered
You
you
can
can
simply
simply
call
call
event
event
emitter
emitter
emit
emit
with
with
that
that
event
event
name,
name
and
and
that
that
triggers
triggers
your
your
callback
callback
function
function
to
to
run.
run
As
as
you
you
can
can
see
see
here,
here
we
we
emit
emit
the
the
event
event
twice,
twice
which
which
will
will
run
run
the
the
callback
callback
function
function
twice.
twice
This
this
event
event
driven
driven
style
style
of
of
programming
programming
is
is
extremely
extremely
useful
useful
when
when
you
you
have
have
something
something
that
that
is
is
CPU
cpu
intensive.
intensive
And
and
with
with
that,
that
we
we
can
can
move
move
on
on
to
to
step
step
6,
6
the
the
file
file
system.
system
Node
node
has
has
a
a
built
built
in
in
file
file
system
system
module
module
called
called
FS.
fs
It
it
can
can
read,
read
write,
write
and
and
delete
delete
files
files
on
on
the
the
file
file
system,
system
among
among
other
other
things,
things
and
and
it
it
can
can
also
also
do
do
things
things
in
in
a
a
blocking
blocking
or
or
non
non
blocking
blocking
way.
way
Allow
allow
me
me
to
to
explain
explain
that
that
by
by
showing
showing
you
you
an
an
example.
example
1st,
1st
create
create
a
a
file
file
on
on
the
the
file
file
system
system
called
called
hello
hello
dot
dot
txt,
txt
and
and
then
then
add
add
whatever
whatever
text
text
you
you
want
want
inside
inside
of
of
it.
it
In
in
our
our
JavaScript
javascript
code,
code
we'll
we'll
import
import
2
2
functions
functions
from
from
the
the
file
file
system
system
module
module
called
called
read
read
file
file
and
and
read
read
file
file
sync.
sync
Anytime
anytime
you
you
see
see
a
a
function
function
that
that
ends
ends
in
in
sync,
sync
think
think
blocking.
blocking
Or
or
in
in
other
other
words,
words
it
it
will
will
need
need
to
to
finish
finish
all
all
of
of
its
its
work
work
before
before
any
any
of
of
your
your
other
other
code
code
can
can
run.
run
We
we
can
can
read
read
a
a
text
text
file
file
in
in
node
node
by
by
simply
simply
passing
passing
the
the
path
path
to
to
that
that
file,
file
and
and
then
then
we'll
we'll
specify
specify
the
the
encoding
encoding
as
as
utf8.
utf8
Now
now
reading
reading
a
a
file
file
might
might
take
take
a
a
while,
while
especially
especially
if
if
it's
it's
a
a
very
very
large
large
file.
file
And
and
what
what
you'll
you'll
notice
notice
here
here
when
when
we
we
run
run
our
our
code
code
is
is
that
that
the
the
second
second
console
console
log
log
won't
won't
run
run
until
until
after
after
that
that
file
file
has
has
been
been
read.
read
Luckily,
luckily
you
you
can
can
make
make
your
your
code
code
non
non
blocking
blocking
by
by
refactoring
refactoring
this
this
to
to
a
a
callback.
callback
With
with
read
read
file,
file
we
we
pass
pass
the
the
same
same
first
first
two
two
arguments
arguments
and
and
then
then
add
add
a
a
callback
callback
function
function
as
as
the
the
3rd
3rd
argument.
argument
Inside
inside
the
the
function,
function
we
we
can
can
access
access
an
an
error
error
object
object
if
if
the
the
operation
operation
fails
fails
or
or
when
when
successful,
successful
the
the
actual
actual
text
text
from
from
the
the
file.
file
The
the
super
super
cool
cool
thing
thing
about
about
this
this
is
is
that
that
even
even
though
though
the
the
console
console
log
log
to
to
the
the
text
text
file
file
comes
comes
first
first
in
in
our
our
script,
script
it's
it's
not
not
the
the
thing
thing
that
that
gets
gets
executed
executed
first.
first
Node
node
registers
registers
the
the
callback,
callback
executes
executes
the
the
rest
rest
of
of
the
the
script,
script
and
and
then
then
finally
finally
runs
runs
the
the
callback
callback
when
when
the
the
file
file
has
has
been
been
read.
read
So
so
that
that
gives
gives
us
us
2
2
different
different
ways
ways
to
to
read
read
a
a
file,
file
But
but
there's
there's
actually
actually
one
one
other
other
way
way
we
we
could
could
go
go
about
about
this,
this
and
and
that's
that's
using
using
a
a
Promise
promise
based
based
solution.
solution
Promises
promises
are
are
also
also
asynchronous
asynchronous
and
and
non
non
blocking,
blocking
and
and
they
they
tend
tend
to
to
produce
produce
much
much
cleaner
cleaner
code
code
when
when
compared
compared
to
to
callbacks.
callbacks
Notice
notice
how
how
in
in
this
this
example,
example
we're
we're
importing
importing
ReadFile
readfile
from
from
the
the
Promises
promises
namespace.
namespace
This
this
gives
gives
us
us
a
a
function
function
that
that
returns
returns
a
a
Promise
promise
when
when
called.
called
If
if
you're
you're
using
using
the
the
latest
latest
bleeding
bleeding
edge
edge
version
version
of
of
node,
node
you
you
could
could
potentially
potentially
use
use
this
this
function
function
with
with
top
top
level
level
await,
await
which
which
means
means
you
you
can
can
use
use
await
await
to
to
resolve
resolve
the
the
promise
promise
here
here
at
at
the
the
top
top
of
of
the
the
script.
script
However,
however
since
since
we're
we're
currently
currently
using
using
Node
node
version
version
12,
12
we'll
we'll
go
go
ahead
ahead
and
and
wrap
wrap
this
this
in
in
an
an
async
async
function.
function
And
and
this
this
async
async
await
await
syntax
syntax
will
will
make
make
your
your
code
code
much
much
easier
easier
to
to
read,
read
especially
especially
when
when
you
you
have
have
multiple
multiple
async
async
calls
calls
in
in
the
the
same
same
function.
function
Okay.
okay
So
so
now
now
that
that
you
you
know
know
how
how
to
to
read
read
files,
files
I
i
want
want
to
to
backtrack
backtrack
a
a
little
little
bit
bit
and
and
talk
talk
about
about
this
this
require
require
function
function
that
that
you
you
see
see
me
me
use
use
in
in
the
the
past
past
couple
couple
examples.
examples
Step
step
7,
7
modules.
modules
A
a
module
module
is
is
nothing
nothing
more
more
than
than
a
a
JavaScript
javascript
file
file
that
that
exports
exports
its
its
code.
code
Node
node
has
has
a
a
bunch
bunch
of
of
built
built
in
in
modules
modules
like
like
f
f
s
s
and
and
events
events
that
that
we've
we've
already
already
looked
looked
at,
at
and
and
there's
there's
a
a
long
long
list
list
of
of
other
other
modules
modules
beyond
beyond
that.
that
The
the
traditional
traditional
way
way
to
to
import
import
a
a
module
module
in
in
Node
node
is
is
to
to
use
use
this
this
require
require
function.
function
But
but
a
a
quick
quick
side
side
note
note
on
on
that
that
first
first
because
because
Node
node
recently
recently
added
added
support
support
for
for
ES
es
modules,
modules
which
which
use
use
the
the
import
import
export
export
syntax.
syntax
Most
most
Node.
node
Js
js
code
code
out
out
there
there
written
written
in
in
vanilla
vanilla
JavaScript
javascript
still
still
uses
uses
require,
require
so
so
it's
it's
very
very
important
important
for
for
you
you
to
to
know
know
as
as
a
a
Node.
node
Js
js
developer.
developer
And
and
if
if
all
all
that's
that's
confusing
confusing
to
to
you,
you
Well,
well
all
all
I
i
can
can
say
say
is
is
welcome
welcome
to
to
the
the
JavaScript
javascript
world.
world
Let's
let's
look
look
at
at
how
how
we
we
can
can
use
use
modules
modules
in
in
our
our
own
own
codebase.
codebase
Create
create
a
a
new
new
file
file
to
to
serve
serve
as
as
your
your
module,
module
Then
then
go
go
into
into
your
your
index
index
JS
js
file,
file
create
create
a
a
variable
variable
for
for
the
the
module,
module
and
and
then
then
import
import
it
it
using
using
require.
require
When
when
you
you
console
console
log
log
it,
it
you'll
you'll
notice
notice
that
that
it's
it's
currently
currently
just
just
an
an
empty
empty
object.
object
In
in
order
order
to
to
make
make
a
a
module
module
useful,
useful
you
you
need
need
to
to
export
export
some
some
code
code
from
from
it.
it
In
in
the
the
module's
module's
file,
file
you
you
can
can
reference
reference
this
this
object
object
with
with
module
module
dot
dot
exports.
exports
You
you
can
can
add
add
new
new
properties
properties
to
to
the
the
object
object
or
or
redefine
redefine
it
it
as
as
a
a
new
new
object.
object
In
in
either
either
case,
case
whatever
whatever
you
you
add
add
here
here
will
will
now
now
be
be
available
available
to
to
use
use
in
in
the
the
other
other
file.
file
And
and
as
as
you
you
can
can
see,
see
the
the
object
object
is
is
no
no
longer
longer
empty
empty
when
when
we
we
console
console
log
log
it.
it
But
but
at
at
some
some
point,
point
you'll
you'll
very
very
likely
likely
want
want
to
to
use
use
somebody
somebody
else's
else's
code
code
out
out
there
there
in
in
the
the
world.
world
And
and
the
the
primary
primary
place
place
to
to
do
do
that
that
is
is
via
via
Node
node
Package
package
Manager
manager
or
or
NPM,
npm
which
which
was
was
recently
recently
acquired
acquired
by
by
GitHub,
github
which
which
itself
itself
was
was
recently
recently
acquired
acquired
by
by
Microsoft.
microsoft
When
when
you
you
installed
installed
Node
node
earlier
earlier
in
in
the
the
video,
video
it
it
also
also
installed
installed
npm,
npm
which
which
is
is
a
a
tool
tool
you
you
can
can
use
use
to
to
install
install
remote
remote
packages
packages
to
to
use
use
in
in
your
your
own
own
code.
code
The
the
first
first
step
step
we'll
we'll
want
want
to
to
take
take
is
is
to
to
open
open
the
the
command
command
line
line
and
and
run
run
npm
npm
init.
init
We'll
we'll
use
use
the
the
y
y
flag
flag
to
to
use
use
the
the
default
default
options,
options
and
and
what
what
you'll
you'll
notice
notice
is
is
that
that
it
it
creates
creates
a
a
package
package
JSON
json
file
file
here
here
in
in
the
the
root
root
of
of
the
the
application.
application
This
this
file
file
can
can
contain
contain
metadata
metadata
about
about
your
your
project,
project
but
but
most
most
importantly,
importantly
it
it
keeps
keeps
track
track
of
of
the
the
dependencies
dependencies
that
that
you
you
use
use
here.
here
At
at
this
this
point,
point
we
we
haven't
haven't
installed
installed
anything
anything
yet.
yet
So
so
let's
let's
go
go
ahead
ahead
and
and
change
change
that
that
by
by
opening
opening
the
the
command
command
line
line
again
again
and
and
running
running
npm
npm
install
install
express.
express
Express
express
is
is
a
a
minimal
minimal
web
web
application
application
framework
framework
and
and
one
one
of
of
the
the
most
most
popular
popular
third
third
party
party
node
node
modules.
modules
After
after
running
running
the
the
command,
command
you'll
you'll
notice
notice
a
a
few
few
things
things
happened
happened
here.
here
In
in
the
the
package
package
JSON
json
file,
file
it
it
added
added
express
express
to
to
the
the
dependencies
dependencies
and
and
pegged
pegged
it
it
to
to
a
a
specific
specific
version.
version
This
this
dependencies
dependencies
object
object
allows
allows
you
you
to
to
manage
manage
multiple
multiple
dependencies
dependencies
in
in
a
a
project
project
and
and
then
then
reinstall
reinstall
them
them
all
all
at
at
once
once
on
on
a
a
different
different
system.
system
Now
now
the
the
actual
actual
raw
raw
source
source
code
code
for
for
the
the
dependency
dependency
lives
lives
in
in
this
this
node
node
modules
modules
directory.
directory
You
you
should
should
never
never
have
have
to
to
modify
modify
code
code
in
in
the
the
node
node
modules
modules
directory.
directory
If
if
you
you
find
find
yourself
yourself
writing
writing
code
code
in
in
here,
here
you're
you're
probably
probably
doing
doing
it
it
wrong.
wrong
That's
that's
because
because
the
the
package
package
JSON
json
controls
controls
how
how
this
this
directory
directory
is
is
built.
built
It
it
fetches
fetches
your
your
remote
remote
packages,
packages
Saves
saves
the
the
source
source
code
code
here
here
and
and
that
that
process
process
should
should
be
be
able
able
to
to
be
be
repeated
repeated
on
on
any
any
system.
system
Now
now
that
that
we
we
have
have
this
this
package
package
installed,
installed
we
we
can
can
import
import
it
it
by
by
name
name
in
in
our
our
JavaScript
javascript
code.
code
In
in
this
this
case,
case
we
we
simply
simply
require
require
Express.
express
And
and
now
now
we've
we've
reached
reached
the
the
point
point
where
where
we're
we're
ready
ready
to
to
put
put
these
these
7
7
steps
steps
together
together
to
to
build
build
a
a
real
real
full
full
stack
stack
application
application
and
and
deploy
deploy
it
it
to
to
the
the
cloud.
cloud
What
what
we're
we're
building
building
here
here
is
is
actually
actually
just
just
your
your
typical
typical
full
full
stack
stack
web
web
application.
application
Our
our
server
server
will
will
live
live
on
on
a
a
URL,
url
and
and
when
when
a
a
user
user
makes
makes
a
a
request
request
to
to
this
this
URL
url
in
in
the
the
browser,
browser
The
the
server
server
will
will
respond
respond
with
with
some
some
HTML.
html
In
in
our
our
code,
code
we'll
we'll
first
first
create
create
an
an
instance
instance
of
of
an
an
Express
express
app.
app
An
an
Express
express
app
app
allows
allows
us
us
to
to
Create
create
different
different
URLs
urls
and
and
endpoints
endpoints
that
that
a
a
user
user
could
could
navigate
navigate
to
to
in
in
the
the
browser.
browser
And
and
then
then
we
we
define
define
code
code
for
for
the
the
server
server
to
to
handle
handle
those
those
requests.
requests
Now
now
I
i
don't
don't
wanna
wanna
get
get
too
too
deep
deep
into
into
HTTP,
http
but
but
when
when
the
the
user
user
navigates
navigates
to
to
a
a
URL
url
in
in
the
the
browser,
browser
it's
it's
what's
what's
known
known
as
as
a
a
get
get
request,
request
which
which
means
means
they're
they're
requesting
requesting
some
some
data
data
on
on
the
the
server
server
and
and
not
not
trying
trying
to
to
modify
modify
or
or
update
update
anything
anything
on
on
the
the
server.
server
With
with
Express,
express
we
we
can
can
set
set
up
up
an
an
endpoint
endpoint
like
like
that
that
by
by
calling
calling
app
app
dot
dot
get.
get
And
and
then
then
the
the
first
first
argument
argument
is
is
the
the
URL
url
that
that
the
the
user
user
will
will
navigate
navigate
to.
to
In
in
this
this
case,
case
we'll
we'll
just
just
use
use
a
a
forward
forward
slash
slash
for
for
the
the
root
root
URL.
url
But
but
feel
feel
free
free
to
to
create
create
multiple
multiple
pages
pages
for
for
your
your
web
web
app
app
by
by
creating
creating
different
different
URLs.
urls
The
the
second
second
argument
argument
here
here
is
is
our
our
callback
callback
function.
function
You
you
can
can
think
think
of
of
every
every
request
request
to
to
this
this
URL
url
as
as
an
an
event,
event
and
and
then
then
you
you
handle
handle
that
that
event
event
with
with
this
this
function.
function
And
and
Express
express
gives
gives
us
us
2
2
parameters
parameters
to
to
make
make
use
use
of,
of
the
the
request
request
and
and
the
the
response.
response
The
the
request
request
is
is
the
the
incoming
incoming
data
data
from
from
the
the
user.
user
In
in
this
this
example,
example
we
we
don't
don't
really
really
need
need
to
to
parse
parse
any
any
data
data
from
from
the
the
request.
request
However,
however
in
in
many
many
cases,
cases
you
you
might
might
wanna
wanna
look
look
at
at
the
the
headers
headers
or
or
the
the
body
body
of
of
the
the
request
request
to
to
authenticate
authenticate
a
a
user
user
or
or
understand
understand
what
what
the
the
user
user
is
is
trying
trying
to
to
do.
do
At
at
this
this
point,
point
we
we
need
need
to
to
implement
implement
the
the
code
code
to
to
handle
handle
the
the
request.
request
What
what
we
we
wanna
wanna
do
do
is
is
read
read
some
some
HTML
html
from
from
our
our
file
file
system
system
and
and
then
then
send
send
it
it
back
back
down
down
to
to
the
the
browser.
browser
As
as
you
you
can
can
see
see
here,
here
I'm
i'm
creating
creating
a
a
file
file
called
called
home
home
dot
dot
HTML
html
with
with
just
just
some
some
generic
generic
HTML
html
markup
markup
inside.
inside
In
in
our
our
source
source
code,
code
we
we
can
can
then
then
import
import
read
read
file
file
from
from
Node's
node's
file
file
system
system
module.
module
Just
just
like
like
we
we
did
did
earlier,
earlier
we'll
we'll
read
read
the
the
file,
file
use
use
UTF
utf
aid
aid
encoding.
encoding
Then
then
in
in
our
our
callback
callback
function
function
here,
here
we'll
we'll
have
have
access
access
to
to
the
the
HTML
html
string,
string
and
and
we
we
can
can
send
send
a
a
response
response
back
back
down
down
to
to
the
the
client
client
by
by
calling
calling
response
response
dot
dot
send.
send
And,
and
also,
also
if
if
there's
there's
an
an
error
error
in
in
the
the
callback,
callback
we
we
can
can
handle
handle
that
that
by
by
sending
sending
a
a
response
response
with
with
a
a
status
status
code
code
of
of
500,
500
which
which
means
means
a
a
server
server
error,
error
so
so
the
the
user
user
knows
knows
that
that
something
something
went
went
wrong
wrong
on
on
the
the
server.
server
And
and
that's
that's
all
all
there
there
is
is
to
to
it.
it
We
we
now
now
have
have
a
a
way
way
to
to
send
send
HTML
html
from
from
the
the
server
server
to
to
the
the
client.
client
Now
now
we
we
just
just
need
need
to
to
tell
tell
our
our
Express
express
app
app
to
to
start
start
listening
listening
to
to
incoming
incoming
requests.
requests
We
we
do
do
that
that
by
by
defining
defining
a
a
port,
port
which
which
will
will
normally
normally
come
come
from
from
a
a
node
node
environment
environment
variable.
variable
Then
then
we
we
call
call
app
app
dot
dot
listen
listen
with
with
that
that
port,
port
and
and
when
when
it
it
starts
starts
up,
up
we'll
we'll
console
console
log
log
that
that
the
the
app
app
is
is
available
available
on
on
local
local
host
host
3,000.
3,000
You
you
You
you
can
can
start
start
it
it
up
up
by
by
opening
opening
the
the
command
command
line
line
and
and
running
running
Node
node
with
with
the
the
current
current
working
working
directory.
directory
If
if
you
you
go
go
ahead
ahead
and
and
open
open
it
it
in
in
the
the
browser,
browser
you
you
should
should
see
see
your
your
HTML
html
return
return
back
back
to
to
you.
you
Now
now
there
there
is
is
one
one
important
important
thing
thing
you
you
should
should
know
know
at
at
this
this
point,
point
and
and
that's
that's
that
that
callbacks
callbacks
can
can
be
be
very
very
difficult
difficult
to
to
work
work
with,
with
especially
especially
as
as
your
your
app
app
grows
grows
in
in
complexity.
complexity
It
it
often
often
leads
leads
to
to
a
a
state
state
known
known
as
as
callback
callback
hell
hell
where
where
you
you
have
have
a
a
bunch
bunch
of
of
callbacks
callbacks
nested
nested
within
within
callbacks,
callbacks
within
within
more
more
callbacks,
callbacks
and
and
so
so
on.
on
A
a
great
great
way
way
to
to
avoid
avoid
code
code
like
like
this
this
is
is
to
to
use
use
promises
promises
instead
instead
of
of
callbacks,
callbacks
and
and
that's
that's
very
very
easy
easy
to
to
do
do
in
in
Node.
node
Js.
js
Instead
instead
of
of
importing
importing
read
read
file
file
from
from
f
f
s,
s
We'll
we'll
import
import
it
it
from
from
f
f
s
s
dot
dot
promises.
promises
We
we
can
can
make
make
our
our
callback
callback
function
function
async,
async
and
and
then
then
we
we
can
can
write
write
the
the
response
response
in
in
a
a
single
single
line
line
of
of
code
code
by
by
saying
saying
response
response
send
send
and
and
then
then
await
await
the
the
operation
operation
to
to
read
read
file.
file
That's
that's
much
much
more
more
concise
concise
and
and
readable,
readable
but
but
it's
it's
especially
especially
useful
useful
when
when
you
you
have
have
multiple
multiple
async
async
operations
operations
to
to
handle
handle
in
in
a
a
single
single
request.
request
So
so
So
so
now
now
that
that
we've
we've
built
built
a
a
Node.
node
Js
js
app,
app
how
how
do
do
we
we
deploy
deploy
it
it
to
to
the
the
cloud
cloud
so
so
people
people
can
can
actually
actually
use
use
it?
it
There's
there's
a
a
bunch
bunch
of
of
different
different
ways
ways
we
we
could
could
do
do
it,
it
but
but
an
an
easy
easy
and
and
free
free
way
way
is
is
with
with
Google
google
App
app
Engine.
engine
App
app
Engine
engine
has
has
what's
what's
known
known
as
as
a
a
standard
standard
environment
environment
for
for
Node.
node
Js
js
up
up
to
to
version
version
12.
12
And
and
what
what
that
that
does
does
is
is
provide
provide
you
you
with
with
a
a
server
server
in
in
the
the
cloud
cloud
that
that
scales
scales
automatically
automatically
based
based
on
on
the
the
incoming
incoming
graphic
graphic
to
to
your
your
app.
app
It's
it's
incredibly
incredibly
easy
easy
to
to
set
set
up,
up
but
but
you
you
first
first
need
need
to
to
have
have
a
a
Google
google
Cloud
cloud
Platform
platform
account
account
and
and
also
also
the
the
Google
google
Cloud
cloud
command
command
line
line
tools
tools
installed
installed
on
on
your
your
local
local
system.
system
Once
once
you
you
have
have
that
that
done,
done
you
you
can
can
simply
simply
go
go
into
into
your
your
source
source
code
code
and
and
create
create
an
an
app
app
dot
dot
YAML
yaml
file.
file
This
this
file
file
is
is
used
used
to
to
configure
configure
your
your
cloud
cloud
server.
server
The
the
only
only
thing
thing
we
we
need
need
at
at
this
this
point
point
is
is
to
to
specify
specify
a
a
runtime
runtime
of
of
Node
node
JS
js
version
version
12.
12
App
app
Engine
engine
will
will
run
run
your
your
code
code
by
by
looking
looking
in
in
the
the
package
package
JSON
json
file
file
for
for
a
a
start
start
script.
script
So
so
let's
let's
go
go
in
in
there
there
and
and
define
define
a
a
start
start
script
script
that
that
runs
runs
node
node
in
in
the
the
current
current
working
working
directory
directory
to
to
start
start
our
our
Express
express
app.
app
From
from
there,
there
we
we
can
can
simply
simply
open
open
the
the
command
command
line
line
and
and
run
run
gcloud
gcloud
app
app
deploy.
deploy
That'll
that'll
take
take
a
a
minute,
minute
and
and
then
then
it
it
will
will
give
give
you
you
a
a
URL
url
where
where
you
you
can
can
access
access
your
your
app
app
publicly
publicly
on
on
the
the
web.
web
Congratulations.
congratulations
You're
you're
now
now
a
a
full
full
stack
stack
cloud
cloud
architect.
architect
If
if
this
this
video
video
helped
helped
you,
you
please
please
like
like
and
and
subscribe.
subscribe
And
and
if
if
you
you
really
really
want
want
to
to
dive
dive
deep
deep
into
into
Node.
node
Js
js
and
and
Express,
express
consider
consider
becoming
becoming
a
a
pro
pro
member
member
at
at
FireShip
fireship
IO.
io
I
i
have
have
a
a
whole
whole
bunch
bunch
of
of
advanced
advanced
content
content
covering
covering
real
real
world
world
use
use
cases
cases
with
with
these
these
technologies.
technologies
Thanks
thanks
for
for
watching,
watching
and
and
I
i
will
will
see
see
you
you
in
in
the
the
next
next
one.
one
success
The speakers discuss the benefits of using variables instead of building a full stack web application and provide instructions on managing variables in Windows and Linux. They also explain how events work in appointed systems and recommend using file system modules and a required function for modules. The speakers provide examples of how to import and export modules, and explain how to set up a callback function and deploy the application to the cloud. They also mention a new framework called App Engine and encourage viewers to like and subscribe to their channel.
Node.js Ultimate Beginner’s Guide in 7 Easy Steps
One of the most valuable skills to have as a full stack web developer is, without a doubt, Node. Js, and that's not going to change anytime soon. But you might be wondering why I'm making a Node tutorial in 2020. Because if you've been watching the news, you may have heard that Node was killed last week by Deno. When I lay my vengeance upon me. Making the extremely chaotic JavaScript world even more convoluted. If you're just starting to learn JavaScript, this can be very confusing, and you might be wondering, should I learn Node or Deno? The answer is Node, especially if you want to get a job or build a product. When something becomes as established as Node, it doesn't get replaced. You'll see things like this happen all the time. A few years ago, everybody was saying GraphQL would replace REST. And 10 years ago, people were saying Node. Js would replace PHP. These predictions never pan out and are just built on top of hype. That being said, I do think Deno is awesome and has a bright future. Both. Both. Both. Both is good. And if you learn Node today, those skills will mostly transfer to Deno tomorrow. In today's video, you'll learn the basics of Node. Js in 7 easy steps. Then we'll put those steps together to build a full stack web application. Because in my opinion, the best way to learn how to code is to build something meaningful from scratch. By the end of the video, we'll deploy our code to the cloud so you can show off your API to your friends and family. If you're new here, like and subscribe and head over to FireShip IO to follow along with the full write up to this video. Step 1 is to understand what Node JS is and why you would wanna use it. Node. Js itself is not a programming language, but rather a runtime that allows you to run JavaScript on a server. You see, when JavaScript first came around in the 19 nineties, It was designed as a simple scripting language to run-in the browser. As the web platform evolved, JavaScript became more and more powerful. And in 2009, we saw the initial release of Node. Js. Now up until this time, it was impossible to write JavaScript code on the server. Most servers were written in languages like Java or PHP. It revolutionized web development because now a web developer could write a full stack application in a single language. So that's all Node. Js is. It's a way for developers to write JavaScript on the server when it could previously only be written in a web browser. Now there are a lot of low level implementation details here that you don't need to know about as a beginner. But if you really wanna dive into how Node. Js works, check out my how it's made video. But what kind of problems can Node. Js actually solve? Node. Js can do a lot. So instead of listing out a bunch of use cases, I'll show you how to build a full stack web application from scratch. This is how it'll work. You visit a URL on the Internet that points to your server. When the request is received, we can use Node to handle the request and read a file from the server's file system and then respond back to the client so they can view the HTML in the browser. Step 2, how to install Node. Js. Node can be installed on Windows, Mac, or Linux and is probably already installed on your system. In this tutorial, I'll be using Ubuntu via the Windows subsystem for Linux. At this point, I've just opened up an empty directory in Versus Code and then pulled up a terminal session. You can find out if Node is installed on your system by running node flag v from the command line. As you can see here, I'm running version 12 dot 16, which is the current long term support version. If If you get an error at this point, it means that you don't have Node installed. And even if you do have Node installed, it'd be a very good idea to use a package called Node Version manager to manage the installation of different node versions on your system. There's an n v m package for Mac and Linux or a separate package for Windows. Go ahead and follow the instructions to install NVM on your system, and that will give you the ability to install any version of Node. Js that you want. For example, you could run nvm install 12 dot 16 dot 3, then run nvm use to use that specific version as your runtime. And now that we have Node JS installed, we can move on to step 3, hello world. But first, if you're serious about Node. Js, consider becoming a pro member at FireShip IO. It'll give you access to my full length courses, almost all of which cover advanced use cases for Node. Js, but we need to walk before we can run. So how do we actually use this thing? One way to mess around with Node is in Repel mode, which stands for read eval print loop. When you type node into the command line, it allows you to run JavaScript code and will print out the results. For example, we can type console log hello world, and it will log that value out. This is nice for messing around, but go ahead and hit control c twice to shut it down. Because in most cases, we'll want to execute JavaScript code that lives in an actual file. The default entry point into a Node. Js app is the index dot JS file. Go ahead and create an index JS file manually in Versus Code. Then inside that file, add console log hello world. You can now run the code inside this file with the node command followed by the path to this file. And because it's an index file, you can actually just point to the parent directory. Congratulations. You just built a node app. Step 4, understand the Node runtime. In most ways, JavaScript works the same way it does in the browser as it does Node. Js, but there are some very important differences you should know about. First, node has a handful of built in identifiers. One of those globals is console, which we've already been using to log out values to the terminal. There's another global with a name of global. This object is a namespace that is available throughout the entire node process. For example, if we assigned a lucky number property to global, We could then access it anywhere else in our code. If you're a front end web developer, you can compare global to the window object in the browser. Now I think the most important global to be familiar with is process, which gives you access to the currently running node process. You might use it to check the current platform or operating system, which in my case is Linux, or grab an environment variable from your server. But this gives us a great opportunity to segue into step 5, how do events work in Node. Js? You'll often hear people describe it as an asynchronous event driven JavaScript runtime. The runtime implements a thing called an event loop, just like a web browser does, and it allows Node to push intensive operations off to a separate thread So only very fast non blocking operations happen on the main thread. And this is why people often call Node non blocking. It's a design choice that makes Node. Js very suitable for things that require high throughput like real time applications, web servers, and things like that. Okay. So how does that affect me as a coder? Well, normally, you won't have to think about the low level details. You just need to know how events and callbacks work. In most cases, you'll listen to events. Events can come in many forms, but one example is on the process global that we looked at earlier. Before a node process finishes, it emits an event named exit. We can listen to this event using on and then register a callback function as the second argument. When the exit event occurs, Node. Js will run this function, and that's where it gets the name callback because the function isn't called the first time Node JS sees it. It's only called after the exit event occurs at some point in the future. This event is built into Node, but you can also create your own from scratch. We'll import an event emitter from the events module that is built into Node, and we'll look at modules in more detail in just a minute. We can create a custom event emitter by instantiating the class and then we'll register a callback that fires on the lunch event. Now that the callback is registered, You can simply call event emitter emit with that event name, and that triggers your callback function to run. As you can see here, we emit the event twice, which will run the callback function twice. This event driven style of programming is extremely useful when you have something that is CPU intensive. And with that, we can move on to step 6, the file system. Node has a built in file system module called FS. It can read, write, and delete files on the file system, among other things, and it can also do things in a blocking or non blocking way. Allow me to explain that by showing you an example. 1st, create a file on the file system called hello dot txt, and then add whatever text you want inside of it. In our JavaScript code, we'll import 2 functions from the file system module called read file and read file sync. Anytime you see a function that ends in sync, think blocking. Or in other words, it will need to finish all of its work before any of your other code can run. We can read a text file in node by simply passing the path to that file, and then we'll specify the encoding as utf8. Now reading a file might take a while, especially if it's a very large file. And what you'll notice here when we run our code is that the second console log won't run until after that file has been read. Luckily, you can make your code non blocking by refactoring this to a callback. With read file, we pass the same first two arguments and then add a callback function as the 3rd argument. Inside the function, we can access an error object if the operation fails or when successful, the actual text from the file. The super cool thing about this is that even though the console log to the text file comes first in our script, it's not the thing that gets executed first. Node registers the callback, executes the rest of the script, and then finally runs the callback when the file has been read. So that gives us 2 different ways to read a file, But there's actually one other way we could go about this, and that's using a Promise based solution. Promises are also asynchronous and non blocking, and they tend to produce much cleaner code when compared to callbacks. Notice how in this example, we're importing ReadFile from the Promises namespace. This gives us a function that returns a Promise when called. If you're using the latest bleeding edge version of node, you could potentially use this function with top level await, which means you can use await to resolve the promise here at the top of the script. However, since we're currently using Node version 12, we'll go ahead and wrap this in an async function. And this async await syntax will make your code much easier to read, especially when you have multiple async calls in the same function. Okay. So now that you know how to read files, I want to backtrack a little bit and talk about this require function that you see me use in the past couple examples. Step 7, modules. A module is nothing more than a JavaScript file that exports its code. Node has a bunch of built in modules like f s and events that we've already looked at, and there's a long list of other modules beyond that. The traditional way to import a module in Node is to use this require function. But a quick side note on that first because Node recently added support for ES modules, which use the import export syntax. Most Node. Js code out there written in vanilla JavaScript still uses require, so it's very important for you to know as a Node. Js developer. And if all that's confusing to you, Well, all I can say is welcome to the JavaScript world. Let's look at how we can use modules in our own codebase. Create a new file to serve as your module, Then go into your index JS file, create a variable for the module, and then import it using require. When you console log it, you'll notice that it's currently just an empty object. In order to make a module useful, you need to export some code from it. In the module's file, you can reference this object with module dot exports. You can add new properties to the object or redefine it as a new object. In either case, whatever you add here will now be available to use in the other file. And as you can see, the object is no longer empty when we console log it. But at some point, you'll very likely want to use somebody else's code out there in the world. And the primary place to do that is via Node Package Manager or NPM, which was recently acquired by GitHub, which itself was recently acquired by Microsoft. When you installed Node earlier in the video, it also installed npm, which is a tool you can use to install remote packages to use in your own code. The first step we'll want to take is to open the command line and run npm init. We'll use the y flag to use the default options, and what you'll notice is that it creates a package JSON file here in the root of the application. This file can contain metadata about your project, but most importantly, it keeps track of the dependencies that you use here. At this point, we haven't installed anything yet. So let's go ahead and change that by opening the command line again and running npm install express. Express is a minimal web application framework and one of the most popular third party node modules. After running the command, you'll notice a few things happened here. In the package JSON file, it added express to the dependencies and pegged it to a specific version. This dependencies object allows you to manage multiple dependencies in a project and then reinstall them all at once on a different system. Now the actual raw source code for the dependency lives in this node modules directory. You should never have to modify code in the node modules directory. If you find yourself writing code in here, you're probably doing it wrong. That's because the package JSON controls how this directory is built. It fetches your remote packages, Saves the source code here and that process should be able to be repeated on any system. Now that we have this package installed, we can import it by name in our JavaScript code. In this case, we simply require Express. And now we've reached the point where we're ready to put these 7 steps together to build a real full stack application and deploy it to the cloud. What we're building here is actually just your typical full stack web application. Our server will live on a URL, and when a user makes a request to this URL in the browser, The server will respond with some HTML. In our code, we'll first create an instance of an Express app. An Express app allows us to Create different URLs and endpoints that a user could navigate to in the browser. And then we define code for the server to handle those requests. Now I don't wanna get too deep into HTTP, but when the user navigates to a URL in the browser, it's what's known as a get request, which means they're requesting some data on the server and not trying to modify or update anything on the server. With Express, we can set up an endpoint like that by calling app dot get. And then the first argument is the URL that the user will navigate to. In this case, we'll just use a forward slash for the root URL. But feel free to create multiple pages for your web app by creating different URLs. The second argument here is our callback function. You can think of every request to this URL as an event, and then you handle that event with this function. And Express gives us 2 parameters to make use of, the request and the response. The request is the incoming data from the user. In this example, we don't really need to parse any data from the request. However, in many cases, you might wanna look at the headers or the body of the request to authenticate a user or understand what the user is trying to do. At this point, we need to implement the code to handle the request. What we wanna do is read some HTML from our file system and then send it back down to the browser. As you can see here, I'm creating a file called home dot HTML with just some generic HTML markup inside. In our source code, we can then import read file from Node's file system module. Just like we did earlier, we'll read the file, use UTF aid encoding. Then in our callback function here, we'll have access to the HTML string, and we can send a response back down to the client by calling response dot send. And, also, if there's an error in the callback, we can handle that by sending a response with a status code of 500, which means a server error, so the user knows that something went wrong on the server. And that's all there is to it. We now have a way to send HTML from the server to the client. Now we just need to tell our Express app to start listening to incoming requests. We do that by defining a port, which will normally come from a node environment variable. Then we call app dot listen with that port, and when it starts up, we'll console log that the app is available on local host 3,000. You You can start it up by opening the command line and running Node with the current working directory. If you go ahead and open it in the browser, you should see your HTML return back to you. Now there is one important thing you should know at this point, and that's that callbacks can be very difficult to work with, especially as your app grows in complexity. It often leads to a state known as callback hell where you have a bunch of callbacks nested within callbacks, within more callbacks, and so on. A great way to avoid code like this is to use promises instead of callbacks, and that's very easy to do in Node. Js. Instead of importing read file from f s, We'll import it from f s dot promises. We can make our callback function async, and then we can write the response in a single line of code by saying response send and then await the operation to read file. That's much more concise and readable, but it's especially useful when you have multiple async operations to handle in a single request. So So now that we've built a Node. Js app, how do we deploy it to the cloud so people can actually use it? There's a bunch of different ways we could do it, but an easy and free way is with Google App Engine. App Engine has what's known as a standard environment for Node. Js up to version 12. And what that does is provide you with a server in the cloud that scales automatically based on the incoming graphic to your app. It's incredibly easy to set up, but you first need to have a Google Cloud Platform account and also the Google Cloud command line tools installed on your local system. Once you have that done, you can simply go into your source code and create an app dot YAML file. This file is used to configure your cloud server. The only thing we need at this point is to specify a runtime of Node JS version 12. App Engine will run your code by looking in the package JSON file for a start script. So let's go in there and define a start script that runs node in the current working directory to start our Express app. From there, we can simply open the command line and run gcloud app deploy. That'll take a minute, and then it will give you a URL where you can access your app publicly on the web. Congratulations. You're now a full stack cloud architect. If this video helped you, please like and subscribe. And if you really want to dive deep into Node. Js and Express, consider becoming a pro member at FireShip IO. I have a whole bunch of advanced content covering real world use cases with these technologies. Thanks for watching, and I will see you in the next one.
The speakers discuss the benefits of using variables instead of building a full stack web application and provide instructions on managing variables in Windows and Linux. They also explain how events work in appointed systems and recommend using file system modules and a required function for modules. The speakers provide examples of how to import and export modules, and explain how to set up a callback function and deploy the application to the cloud. They also mention a new framework called App Engine and encourage viewers to like and subscribe to their channel.

        [INST]
        You are youtuber called Fireship you make engaging high-intensity and entertaining coding tutorials and tech news. 
        you covers a wide range of topics relevant to programmers, aiming to help them learn and improve their skills quickly.
        
        Given the title of the video : Node.js Ultimate Beginner’s Guide in 7 Easy Steps 
        and a small summary : The speakers discuss the benefits of using variables instead of building a full stack web application and provide instructions on managing variables in Windows and Linux. They also explain how events work in appointed systems and recommend using file system modules and a required function for modules. The speakers provide examples of how to import and export modules, and explain how to set up a callback function and deploy the application to the cloud. They also mention a new framework called App Engine and encourage viewers to like and subscribe to their channel.
        [/INST]
        
        Generate the video : One of the most valuable skills to have as a full stack web developer is, without a doubt, Node. Js, and that's not going to change anytime soon. But you might be wondering why I'm making a Node tutorial in 2020. Because if you've been watching the news, you may have heard that Node was killed last week by Deno. When I lay my vengeance upon me. Making the extremely chaotic JavaScript world even more convoluted. If you're just starting to learn JavaScript, this can be very confusing, and you might be wondering, should I learn Node or Deno? The answer is Node, especially if you want to get a job or build a product. When something becomes as established as Node, it doesn't get replaced. You'll see things like this happen all the time. A few years ago, everybody was saying GraphQL would replace REST. And 10 years ago, people were saying Node. Js would replace PHP. These predictions never pan out and are just built on top of hype. That being said, I do think Deno is awesome and has a bright future. Both. Both. Both. Both is good. And if you learn Node today, those skills will mostly transfer to Deno tomorrow. In today's video, you'll learn the basics of Node. Js in 7 easy steps. Then we'll put those steps together to build a full stack web application. Because in my opinion, the best way to learn how to code is to build something meaningful from scratch. By the end of the video, we'll deploy our code to the cloud so you can show off your API to your friends and family. If you're new here, like and subscribe and head over to FireShip IO to follow along with the full write up to this video. Step 1 is to understand what Node JS is and why you would wanna use it. Node. Js itself is not a programming language, but rather a runtime that allows you to run JavaScript on a server. You see, when JavaScript first came around in the 19 nineties, It was designed as a simple scripting language to run-in the browser. As the web platform evolved, JavaScript became more and more powerful. And in 2009, we saw the initial release of Node. Js. Now up until this time, it was impossible to write JavaScript code on the server. Most servers were written in languages like Java or PHP. It revolutionized web development because now a web developer could write a full stack application in a single language. So that's all Node. Js is. It's a way for developers to write JavaScript on the server when it could previously only be written in a web browser. Now there are a lot of low level implementation details here that you don't need to know about as a beginner. But if you really wanna dive into how Node. Js works, check out my how it's made video. But what kind of problems can Node. Js actually solve? Node. Js can do a lot. So instead of listing out a bunch of use cases, I'll show you how to build a full stack web application from scratch. This is how it'll work. You visit a URL on the Internet that points to your server. When the request is received, we can use Node to handle the request and read a file from the server's file system and then respond back to the client so they can view the HTML in the browser. Step 2, how to install Node. Js. Node can be installed on Windows, Mac, or Linux and is probably already installed on your system. In this tutorial, I'll be using Ubuntu via the Windows subsystem for Linux. At this point, I've just opened up an empty directory in Versus Code and then pulled up a terminal session. You can find out if Node is installed on your system by running node flag v from the command line. As you can see here, I'm running version 12 dot 16, which is the current long term support version. If If you get an error at this point, it means that you don't have Node installed. And even if you do have Node installed, it'd be a very good idea to use a package called Node Version manager to manage the installation of different node versions on your system. There's an n v m package for Mac and Linux or a separate package for Windows. Go ahead and follow the instructions to install NVM on your system, and that will give you the ability to install any version of Node. Js that you want. For example, you could run nvm install 12 dot 16 dot 3, then run nvm use to use that specific version as your runtime. And now that we have Node JS installed, we can move on to step 3, hello world. But first, if you're serious about Node. Js, consider becoming a pro member at FireShip IO. It'll give you access to my full length courses, almost all of which cover advanced use cases for Node. Js, but we need to walk before we can run. So how do we actually use this thing? One way to mess around with Node is in Repel mode, which stands for read eval print loop. When you type node into the command line, it allows you to run JavaScript code and will print out the results. For example, we can type console log hello world, and it will log that value out. This is nice for messing around, but go ahead and hit control c twice to shut it down. Because in most cases, we'll want to execute JavaScript code that lives in an actual file. The default entry point into a Node. Js app is the index dot JS file. Go ahead and create an index JS file manually in Versus Code. Then inside that file, add console log hello world. You can now run the code inside this file with the node command followed by the path to this file. And because it's an index file, you can actually just point to the parent directory. Congratulations. You just built a node app. Step 4, understand the Node runtime. In most ways, JavaScript works the same way it does in the browser as it does Node. Js, but there are some very important differences you should know about. First, node has a handful of built in identifiers. One of those globals is console, which we've already been using to log out values to the terminal. There's another global with a name of global. This object is a namespace that is available throughout the entire node process. For example, if we assigned a lucky number property to global, We could then access it anywhere else in our code. If you're a front end web developer, you can compare global to the window object in the browser. Now I think the most important global to be familiar with is process, which gives you access to the currently running node process. You might use it to check the current platform or operating system, which in my case is Linux, or grab an environment variable from your server. But this gives us a great opportunity to segue into step 5, how do events work in Node. Js? You'll often hear people describe it as an asynchronous event driven JavaScript runtime. The runtime implements a thing called an event loop, just like a web browser does, and it allows Node to push intensive operations off to a separate thread So only very fast non blocking operations happen on the main thread. And this is why people often call Node non blocking. It's a design choice that makes Node. Js very suitable for things that require high throughput like real time applications, web servers, and things like that. Okay. So how does that affect me as a coder? Well, normally, you won't have to think about the low level details. You just need to know how events and callbacks work. In most cases, you'll listen to events. Events can come in many forms, but one example is on the process global that we looked at earlier. Before a node process finishes, it emits an event named exit. We can listen to this event using on and then register a callback function as the second argument. When the exit event occurs, Node. Js will run this function, and that's where it gets the name callback because the function isn't called the first time Node JS sees it. It's only called after the exit event occurs at some point in the future. This event is built into Node, but you can also create your own from scratch. We'll import an event emitter from the events module that is built into Node, and we'll look at modules in more detail in just a minute. We can create a custom event emitter by instantiating the class and then we'll register a callback that fires on the lunch event. Now that the callback is registered, You can simply call event emitter emit with that event name, and that triggers your callback function to run. As you can see here, we emit the event twice, which will run the callback function twice. This event driven style of programming is extremely useful when you have something that is CPU intensive. And with that, we can move on to step 6, the file system. Node has a built in file system module called FS. It can read, write, and delete files on the file system, among other things, and it can also do things in a blocking or non blocking way. Allow me to explain that by showing you an example. 1st, create a file on the file system called hello dot txt, and then add whatever text you want inside of it. In our JavaScript code, we'll import 2 functions from the file system module called read file and read file sync. Anytime you see a function that ends in sync, think blocking. Or in other words, it will need to finish all of its work before any of your other code can run. We can read a text file in node by simply passing the path to that file, and then we'll specify the encoding as utf8. Now reading a file might take a while, especially if it's a very large file. And what you'll notice here when we run our code is that the second console log won't run until after that file has been read. Luckily, you can make your code non blocking by refactoring this to a callback. With read file, we pass the same first two arguments and then add a callback function as the 3rd argument. Inside the function, we can access an error object if the operation fails or when successful, the actual text from the file. The super cool thing about this is that even though the console log to the text file comes first in our script, it's not the thing that gets executed first. Node registers the callback, executes the rest of the script, and then finally runs the callback when the file has been read. So that gives us 2 different ways to read a file, But there's actually one other way we could go about this, and that's using a Promise based solution. Promises are also asynchronous and non blocking, and they tend to produce much cleaner code when compared to callbacks. Notice how in this example, we're importing ReadFile from the Promises namespace. This gives us a function that returns a Promise when called. If you're using the latest bleeding edge version of node, you could potentially use this function with top level await, which means you can use await to resolve the promise here at the top of the script. However, since we're currently using Node version 12, we'll go ahead and wrap this in an async function. And this async await syntax will make your code much easier to read, especially when you have multiple async calls in the same function. Okay. So now that you know how to read files, I want to backtrack a little bit and talk about this require function that you see me use in the past couple examples. Step 7, modules. A module is nothing more than a JavaScript file that exports its code. Node has a bunch of built in modules like f s and events that we've already looked at, and there's a long list of other modules beyond that. The traditional way to import a module in Node is to use this require function. But a quick side note on that first because Node recently added support for ES modules, which use the import export syntax. Most Node. Js code out there written in vanilla JavaScript still uses require, so it's very important for you to know as a Node. Js developer. And if all that's confusing to you, Well, all I can say is welcome to the JavaScript world. Let's look at how we can use modules in our own codebase. Create a new file to serve as your module, Then go into your index JS file, create a variable for the module, and then import it using require. When you console log it, you'll notice that it's currently just an empty object. In order to make a module useful, you need to export some code from it. In the module's file, you can reference this object with module dot exports. You can add new properties to the object or redefine it as a new object. In either case, whatever you add here will now be available to use in the other file. And as you can see, the object is no longer empty when we console log it. But at some point, you'll very likely want to use somebody else's code out there in the world. And the primary place to do that is via Node Package Manager or NPM, which was recently acquired by GitHub, which itself was recently acquired by Microsoft. When you installed Node earlier in the video, it also installed npm, which is a tool you can use to install remote packages to use in your own code. The first step we'll want to take is to open the command line and run npm init. We'll use the y flag to use the default options, and what you'll notice is that it creates a package JSON file here in the root of the application. This file can contain metadata about your project, but most importantly, it keeps track of the dependencies that you use here. At this point, we haven't installed anything yet. So let's go ahead and change that by opening the command line again and running npm install express. Express is a minimal web application framework and one of the most popular third party node modules. After running the command, you'll notice a few things happened here. In the package JSON file, it added express to the dependencies and pegged it to a specific version. This dependencies object allows you to manage multiple dependencies in a project and then reinstall them all at once on a different system. Now the actual raw source code for the dependency lives in this node modules directory. You should never have to modify code in the node modules directory. If you find yourself writing code in here, you're probably doing it wrong. That's because the package JSON controls how this directory is built. It fetches your remote packages, Saves the source code here and that process should be able to be repeated on any system. Now that we have this package installed, we can import it by name in our JavaScript code. In this case, we simply require Express. And now we've reached the point where we're ready to put these 7 steps together to build a real full stack application and deploy it to the cloud. What we're building here is actually just your typical full stack web application. Our server will live on a URL, and when a user makes a request to this URL in the browser, The server will respond with some HTML. In our code, we'll first create an instance of an Express app. An Express app allows us to Create different URLs and endpoints that a user could navigate to in the browser. And then we define code for the server to handle those requests. Now I don't wanna get too deep into HTTP, but when the user navigates to a URL in the browser, it's what's known as a get request, which means they're requesting some data on the server and not trying to modify or update anything on the server. With Express, we can set up an endpoint like that by calling app dot get. And then the first argument is the URL that the user will navigate to. In this case, we'll just use a forward slash for the root URL. But feel free to create multiple pages for your web app by creating different URLs. The second argument here is our callback function. You can think of every request to this URL as an event, and then you handle that event with this function. And Express gives us 2 parameters to make use of, the request and the response. The request is the incoming data from the user. In this example, we don't really need to parse any data from the request. However, in many cases, you might wanna look at the headers or the body of the request to authenticate a user or understand what the user is trying to do. At this point, we need to implement the code to handle the request. What we wanna do is read some HTML from our file system and then send it back down to the browser. As you can see here, I'm creating a file called home dot HTML with just some generic HTML markup inside. In our source code, we can then import read file from Node's file system module. Just like we did earlier, we'll read the file, use UTF aid encoding. Then in our callback function here, we'll have access to the HTML string, and we can send a response back down to the client by calling response dot send. And, also, if there's an error in the callback, we can handle that by sending a response with a status code of 500, which means a server error, so the user knows that something went wrong on the server. And that's all there is to it. We now have a way to send HTML from the server to the client. Now we just need to tell our Express app to start listening to incoming requests. We do that by defining a port, which will normally come from a node environment variable. Then we call app dot listen with that port, and when it starts up, we'll console log that the app is available on local host 3,000. You You can start it up by opening the command line and running Node with the current working directory. If you go ahead and open it in the browser, you should see your HTML return back to you. Now there is one important thing you should know at this point, and that's that callbacks can be very difficult to work with, especially as your app grows in complexity. It often leads to a state known as callback hell where you have a bunch of callbacks nested within callbacks, within more callbacks, and so on. A great way to avoid code like this is to use promises instead of callbacks, and that's very easy to do in Node. Js. Instead of importing read file from f s, We'll import it from f s dot promises. We can make our callback function async, and then we can write the response in a single line of code by saying response send and then await the operation to read file. That's much more concise and readable, but it's especially useful when you have multiple async operations to handle in a single request. So So now that we've built a Node. Js app, how do we deploy it to the cloud so people can actually use it? There's a bunch of different ways we could do it, but an easy and free way is with Google App Engine. App Engine has what's known as a standard environment for Node. Js up to version 12. And what that does is provide you with a server in the cloud that scales automatically based on the incoming graphic to your app. It's incredibly easy to set up, but you first need to have a Google Cloud Platform account and also the Google Cloud command line tools installed on your local system. Once you have that done, you can simply go into your source code and create an app dot YAML file. This file is used to configure your cloud server. The only thing we need at this point is to specify a runtime of Node JS version 12. App Engine will run your code by looking in the package JSON file for a start script. So let's go in there and define a start script that runs node in the current working directory to start our Express app. From there, we can simply open the command line and run gcloud app deploy. That'll take a minute, and then it will give you a URL where you can access your app publicly on the web. Congratulations. You're now a full stack cloud architect. If this video helped you, please like and subscribe. And if you really want to dive deep into Node. Js and Express, consider becoming a pro member at FireShip IO. I have a whole bunch of advanced content covering real world use cases with these technologies. Thanks for watching, and I will see you in the next one.
        