What is Node? Intro to Node File System with JavaScript fs
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.
This lesson preview is part of the TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL, plus 70+ \newline books, guides and courses with the \newline Pro subscription.
[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 ]