Node.js – Introduction
What is Node.js?
Node.js is an open-source, cross-platform JavaScript runtime environment used for executing JavaScript code outside of a web browser.
Node.js is a great web framework for beginners because it works great for data-intensive applications, like streaming and real-time apps, and Node.js makes it easy to start building the back-end.
Node.js allows us to use JavaScript everywhere and on any browser, including MacOS, Linux, and Windows. When we say everywhere, we mean the front-end, the middle-ware, and the back-end. Node.js is, therefore, part of some very popular web development stacks, such as the MERN stack, MEVN stack, and MEAN stack.
There are a number of characteristics that make Node.js what it is:
- Google Chrome V8 JavaScript Engine: This runtime environment is built on the Google Chrome V8 JavaScript runtime engine. In the same way a Java Virtual Machine translates bytecode, the Chrome V8 JavaScript engine takes JavaScript and makes it readable.
- Modules/Packages: Node.js has npm, a node package manager, with a library of over 350,000 packages to help get your project or application off the ground with efficiency and ease.
- Event Driven, Single-Threaded I/O Model: JavaScript relies on user interactions or events to run. In most cases, code is run synchronously. Server requests and other such asynchronous tasks rely on a system of promises or async/await functions to handle these inputs and outputs.
Fundamentals of Node.js
Now that we know what Node.js is, let’s explore the fundamentals of this tool.
Console
The console is a module provided by Node.js that is akin to the JavaScript console in the browser when you inspect a webpage. The console has methods that are available for us to use for debugging purposes:
console.log()
: Frequently used to log some sort of output.
console.warn()
: Explicitly delivers a warning to the console.
console.error()
: Explicitly delivers an error message to the console. You can log an error as a string or as an object. If logged as a newError()
, a traceback will be included as part of the message.
console.trace()
: Logs a traceback when an error occurs in your code. Gives line number and column number of the file that the error probably occurred.
Buffer
At its core, the Buffer class in Node.js is a temporary storage solution for file systems. Due to its low-level nature, as web developers we will rarely actually use the Buffer class directly. The main purpose of this class is to allocate memory.
Let’s take a look at a few methods that the Buffer class provides.
File System
The file system (fs) module allows us to interact with files in Node.js. There are synchronous and asynchronous methods that can be used to read or write to a file using the fs module. In contrast to using console or the Buffer class, we need to import the fs module into the file that we would like to use in order to get it to work.
The code example below shows how the readFile
, an asynchronous method, works. Notice that the last argument in the method is a callback function whose first argument is an error. By definition this callback will always pass in the error first before the data.