What is Node.js? A Beginner's Guide.
Node is a JavaScript runtime environment that was first introduced in 2009 by Ryan Dahl, as a response to how slow web servers were at the time. In this lesson, we'll introduce Node and talk about the capability Node provides to make I/O tasks asynchronous and non-blocking.
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
What is Node?
📝 This lesson's quiz can be found - here.
🗒️ Solutions for this lesson's quiz can be found - here.
📖 This lesson's lecture slides can be found - here.
Node
Node is a JavaScript runtime environment that can run on different platforms (Mac, Windows, Linux, etc.). What this means is JavaScript (which was originally created to run inside a web browser) can now be run on any computer as a web server.
Node was originally released in 2009 by Ryan Dahl as a response to how slow web servers were at the time. This is because most web servers would block the I/O (Input/Output) task (e.g. reading from the file system or accessing the network) which will lower throughput. Node changed this model by making all I/O tasks non-blocking and asynchronous. Non-blocking, for example, just means a request from another interaction can be processed without waiting for the prior interaction request to finish. This allowed web servers to serve countless requests concurrently.
Non-blocking I/O
Here's an example taken from the main Node website in comparing code between the synchronous blocking state and the asynchronous non-blocking state.
This example covers the use of the Node File System (fs
module) which allows us to work with the file system in our computer.
const fs = require("fs");
const data = fs.readFileSync("/file.md");
moreWork();
The file system module is included by stating require('fs')
. The readFileSync()
method is used to read files on the computer with which we've stated we want to read the markdown file named file.md
. The readFileSync()
method is synchronous so the moreWork()
function will only run after the entire file is read in the process. Since the readFileSync()
method blocks moreWork()
from running, this is an example of synchronous blocking code.
As we've mentioned, Node allows I/O taks to be non-blocking and asynchronous and as a result provides asynchronous forms for all file system operations. Here's an attempt to read the file.md
file and run the moreWork()
function in an asynchronous setting:
const fs = require("fs");
let data;
fs.readFile("/file.md", (err, res) => {
if (err) throw err;
data = res;
});
moreWork();
The readFile()
method is asynchronous and allows the use of a callback function. The callback function won't run until the former function, readFile()
, is complete. In this case, the readFile()
method doesn't block code since the moreWork()
function will be run while the file is being read. Whenever the reading of the file is complete, the callback function will then run.
The ability to have the moreWork()
function run alongside the reading of a file, in the asynchronous example above, was a primary design choice in Node to allow for higher throughput. Callback functions, promises, and the use of the async/await
syntax are some of the ways in which Node allows us to conduct and use asynchronous functions.
Node and npm
If you've built any web application within a Node environment, you've already come across something known as npm
. npm
, short for Node Package Manager, is two things:
- An online repository for the publishing of open-source Node Projects.
- A command-line utility for interacting with the
npm
repository.
The ecosystem of third-party tools that can easily be installed in a Node application makes Node a very rich ecosystem. This also ties in with how we build applications in Node by introducing just the tools and libraries we need in an app.
One awesome benefit of using Node is that Node makes capable the building of universal JavaScript applications (often also known as isomorphic JavaScript). These are applications that have JavaScript both on the client and the server which is exactly what we'll be doing in our course!
Node is built against modern versions of V8 (Google's JavaScript and WebAssembly engine) which helps ensure Node stays mostly up to date with the latest ECMA JavaScript syntax. Node already supports a vast majority of ES6 features which can be seen in the node.green website.
To install Node, all we have to do is go to https://nodejs.org and download the latest LTS version of Node (which is recommended for most users).
An easy way to verify if Node is installed on a computer is to run the following command in the terminal to determine the version of Node installed.
node -v
npm
is installed as part of Node with which we can also check for the version of npm
.
npm -v
Lesson Transcript
[00:00 - 00:08] Node is a JavaScript runtime environment that can run on different platforms. Mac, Windows, Linux.
[00:09 - 00:27] What this means is JavaScript which is originally created to run inside a web browser can now be run on any computer as a web server. Node was originally released in 2009 by Ryan Dahl as a response to how slow web servers were at the time.
[00:28 - 00:41] This is because most web servers would block the input output task which would lower throughputs. Node changed this model by making all input output tasks non-blocking and asynchronous.
[00:42 - 00:58] Non-blocking just means that a request from a certain interaction can be processed without waiting for the prior interaction request to finish. This allowed web servers to serve countless requests concurrently.
[00:59 - 01:15] Here's an example taken from the main Node website in comparing code between the synchronous blocking state and the asynchronous non-blocking state. This example covers the use of the Node file system which allows us to work with the file system in our computer.
[01:16 - 01:34] The file system module is included by stating require FS which is short for file system. The read file sync method is used to read files in the computer with which here we're stating that we want to read a file named file.md.
[01:35 - 01:48] The read file sync method is synchronous so here the more work function would only run after the entire file is read in the process above. This is an example of synchronous blocking code.
[01:49 - 02:03] The second line here read file sync blocks the code from continuing until its process is complete. As we've mentioned, node allows input output tasks to be non-blocking and asynchronous.
[02:04 - 02:24] So in this example, we're doing the exact same thing that is to say reading a file named filemd and running the more work function but we're doing so in a non-blocking setting. This is because we're now using a read file method which is asynchronous and allows the use of a callback function.
[02:25 - 02:40] This callback function won't run until the former function read file is complete. In this case read file doesn't block code since the more work function will be run while the file is being read.
[02:41 - 02:59] Whenever the reading of the file is complete, the callback function will then run. The ability to have the more work function be run alongside the reading of a file was a primary design choice in node to allow for higher throughputs.
[03:00 - 03:20] Callback functions, promises and the capability to use async/await are all the tools that node gives us to have input output tasks be asynchronous and non-blocking. If you've built any web application within a node environment, you've already come across something known as NPM.
[03:21 - 03:34] NPM short for node package manager is two things. It's an online repository for the publishing of open source node projects and it's a command line utility for interacting with the NPM repository.
[03:35 - 03:50] Now the ecosystem of third party tools that can be easily installed in a node application makes the node a very rich ecosystem. This also ties in with how we build applications in node by introducing just the tools and libraries we need in an app.
[03:51 - 04:11] One awesome benefit of using node is that node makes capable the building of universal JavaScript applications, often also known as isomorphic JavaScript. These are applications that have JavaScript both on the client and the server and this is exactly what we'll be doing in this course.
[04:12 - 04:34] Node is built against modern versions of V8, Google's JavaScript and Web Assembly engine, which helps ensure node stays up to date with the latest ECMA JavaScript syntax . To install node, all we have to do is go to nodejs.org and download the latest LTS version of node, which is recommended for most users.
[04:35 - 04:52] Once downloaded, we just follow the onscreen instructions to complete the installation. An easy way to verify if node is installed in your computer is to run the following command of the terminal, node-v, which will tell you the version of node installed.
[04:53 - 05:28] NPM is installed as part of node, but to also verify its availability, we can check for its version number with NPM-V. [ Silence ]
[00:00 - 00:08] Node is a JavaScript runtime environment that can run on different platforms. Mac, Windows, Linux.
[00:09 - 00:27] What this means is JavaScript which is originally created to run inside a web browser can now be run on any computer as a web server. Node was originally released in 2009 by Ryan Dahl as a response to how slow web servers were at the time.
[00:28 - 00:41] This is because most web servers would block the input output task which would lower throughputs. Node changed this model by making all input output tasks non-blocking and asynchronous.
[00:42 - 00:58] Non-blocking just means that a request from a certain interaction can be processed without waiting for the prior interaction request to finish. This allowed web servers to serve countless requests concurrently.
[00:59 - 01:15] Here's an example taken from the main Node website in comparing code between the synchronous blocking state and the asynchronous non-blocking state. This example covers the use of the Node file system which allows us to work with the file system in our computer.
[01:16 - 01:34] The file system module is included by stating require FS which is short for file system. The read file sync method is used to read files in the computer with which here we're stating that we want to read a file named file.md.
[01:35 - 01:48] The read file sync method is synchronous so here the more work function would only run after the entire file is read in the process above. This is an example of synchronous blocking code.
[01:49 - 02:03] The second line here read file sync blocks the code from continuing until its process is complete. As we've mentioned, node allows input output tasks to be non-blocking and asynchronous.
[02:04 - 02:24] So in this example, we're doing the exact same thing that is to say reading a file named filemd and running the more work function but we're doing so in a non-blocking setting. This is because we're now using a read file method which is asynchronous and allows the use of a callback function.
[02:25 - 02:40] This callback function won't run until the former function read file is complete. In this case read file doesn't block code since the more work function will be run while the file is being read.
[02:41 - 02:59] Whenever the reading of the file is complete, the callback function will then run. The ability to have the more work function be run alongside the reading of a file was a primary design choice in node to allow for higher throughputs.
[03:00 - 03:20] Callback functions, promises and the capability to use async/await are all the tools that node gives us to have input output tasks be asynchronous and non-blocking. If you've built any web application within a node environment, you've already come across something known as NPM.
[03:21 - 03:34] NPM short for node package manager is two things. It's an online repository for the publishing of open source node projects and it's a command line utility for interacting with the NPM repository.
[03:35 - 03:50] Now the ecosystem of third party tools that can be easily installed in a node application makes the node a very rich ecosystem. This also ties in with how we build applications in node by introducing just the tools and libraries we need in an app.
[03:51 - 04:11] One awesome benefit of using node is that node makes capable the building of universal JavaScript applications, often also known as isomorphic JavaScript. These are applications that have JavaScript both on the client and the server and this is exactly what we'll be doing in this course.
[04:12 - 04:34] Node is built against modern versions of V8, Google's JavaScript and Web Assembly engine, which helps ensure node stays up to date with the latest ECMA JavaScript syntax . To install node, all we have to do is go to nodejs.org and download the latest LTS version of node, which is recommended for most users.
[04:35 - 04:52] Once downloaded, we just follow the onscreen instructions to complete the installation. An easy way to verify if node is installed in your computer is to run the following command of the terminal, node-v, which will tell you the version of node installed.
[04:53 - 05:28] NPM is installed as part of node, but to also verify its availability, we can check for its version number with NPM-V. [ Silence ]