No history yet

Introduction to Node.js

JavaScript Beyond the Browser

For a long time, JavaScript lived exclusively inside web browsers. Its job was to make websites interactive—things like validating forms, creating animations, and loading new data without a full page refresh. But in 2009, a developer named Ryan Dahl had an idea: what if you could use JavaScript for more than just websites? What if you could use it to build the software that runs on servers?

This idea led to the creation of Node.js. It's not a new programming language, but rather a different environment where the JavaScript language can run.

Node.js is a JavaScript runtime built on Google’s open-source V8 JavaScript engine.

The V8 engine is the same powerful engine that runs JavaScript inside the Google Chrome browser. Node.js takes that engine and allows it to run on your computer as a standalone application. This means you can use JavaScript to write command-line tools, web servers, and other backend services that handle the behind-the-scenes logic of an application.

How It Works

Node.js was designed to be efficient at handling many tasks at once. It achieves this through two key features: a non-blocking I/O model and an event-driven architecture.

I/O stands for Input/Output. It refers to any operation where your program needs to interact with something outside of itself, like reading a file from the disk, making a request over the network, or querying a database. These tasks can be slow.

A blocking approach would mean the program stops and waits for the slow task to finish before doing anything else. Imagine a chef who puts a cake in the oven and then just stands there watching it bake for an hour, refusing to chop vegetables or wash dishes until the cake is done. Not very efficient.

Node.js uses a non-blocking approach. The chef puts the cake in the oven, sets a timer, and then immediately starts working on other tasks. When the timer goes off (an event), the chef stops what they're doing, takes the cake out, and then resumes their other work. This is the essence of Node.js's efficiency. It can handle thousands of connections simultaneously because it doesn't wait around.

Getting Started

To start using Node.js, you first need to install it on your computer. The installation is straightforward and also includes another essential tool called npm.

npm

noun

Node Package Manager. It's a massive online library of open-source code packages (or modules) that you can easily add to your projects, and a command-line tool to interact with that library.

You can download the installer from the official Node.js website, nodejs.org. You'll see two versions available: LTS and Current. For most users, LTS (Long-Term Support) is the recommended choice because it's the most stable version.

After running the installer, you can verify that Node.js and npm are ready to go. Open your terminal (or Command Prompt on Windows) and run these two commands:

# Check the Node.js version
node -v

# Check the npm version
npm -v

If the commands return version numbers (like v20.11.0), you're all set! Your development environment is simply a text editor for writing code (like Visual Studio Code, Sublime Text, or Atom) and your terminal for running it.

Your First Node.js Script

Let's create a simple "Hello, World!" script to see Node.js in action.

  1. Create a new file and name it app.js.
  2. Add the following line of JavaScript code to the file:
console.log("Hello from Node.js!");

This is the same console.log function you might have used in a browser's developer tools. Here, it will print the message directly to your terminal.

  1. Save the file. In your terminal, navigate to the directory where you saved app.js.
  2. Run the script using the node command:
node app.js

You should see the message "Hello from Node.js!" printed in your terminal. You've just run JavaScript code outside of a web browser.

Quiz Questions 1/5

What was the primary motivation behind the creation of Node.js?

Quiz Questions 2/5

Node.js is built on Google's high-performance V8 engine, which is the same engine used by the Chrome browser.

That's the core idea of Node.js. It's a powerful tool that took JavaScript from a browser-only language to a versatile language capable of powering the backend of complex applications.