Node.js – Introduction
What is Node.js?
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.
There are a number of characteristics that make Node.js what it is:
- 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.
Fundamentals of Node.js
Now that we know what Node.js is, let’s explore the fundamentals of this tool.
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 new
Error(), 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.
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.
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.