Node.js Basics
What exactly is Node.js? It is not a programming language, and it is not a framework. It is a Runtime Environment. It takes the same engine that powers Google Chrome (the V8 engine) and lets it run directly on your computer's operating system.
1. Why Node.js?
At the Hub, we love Node.js for three main reasons:
- JavaScript Everywhere: You don't need to learn a new language like Python or PHP for the backend.
- Blazing Fast: Node.js is designed for "non-blocking" operations, making it perfect for real-time apps like chat or live cricket score updates.
- NPM (Node Package Manager): Access to millions of ready-to-use code libraries created by the global community.
2. Checking your Environment
Before we start, make sure you have Node.js installed. Open your terminal and type:
node -v
If you see a version number (like v20.x.x), you are ready to go!
3. Running your first Node Script
In the frontend, you had to link a .js file to an .html file to see it work. In Node, you run it directly.
-
Create a file named
hello.js. -
Write the following code:
hello.jsconsole.log("Welcome to the Backend, A Master!");
const colors = ["Red", "Green", "Blue"];
console.log("Your available colors are:", colors); -
In your terminal, run:
node hello.js
4. The Global Object
In the browser, the top-level object is window. In Node.js, there is no window (because there is no browser!). Instead, the top-level object is called global.
| Feature | Browser (Frontend) | Node.js (Backend) |
|---|---|---|
| Global Object | window | global |
| DOM | Access to document | No DOM |
| File System | Blocked for security | Can read/write files |
| Location | location.href | process.cwd() |
5. Modules: Sharing Code
Node.js uses a system called CommonJS to share code between files. You use require to import code and module.exports to send it out.
const add = (a, b) => a + b;
module.exports = add;
And then in another file, you can use it:
const add = require('./math');
console.log("2 + 3 =", add(2, 3)); // Output: 2 + 3 = 5
6. Core Modules
Node.js comes with built-in "Superpowers" called Core Modules. You don't need to install them; you just import them.
fs(File System): To read and write files on your computer.path: To handle file and folder paths.http: To create a basic web server.
Example: Reading a File
const fs = require('fs');
fs.readFile('note.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log("File content:", data);
});
In the above example, if note.txt does not exist, the err object will contain the error details. Throwing an error inside an asynchronous callback will cause the Node.js process to crash. Instead of throwing, you should log the error or handle it gracefully to keep the server running.
fs.readFile('note.txt', 'utf8', (err, data) => {
if (err) {
console.error("Error reading file:", err);
return;
}
console.log("File content:", data);
});
Practice: The Greeting Machine
- Create a file named
greet.js. - Use
process.argvto get a name from the terminal command. - Run it like this:
node greet.js Ajay.
const name = process.argv[2] || "Guest";
console.log(`Hello, ${name}! Welcome to CodeHarborHub.`);
Always start a new project by running npm init -y. This creates a package.json file, which is like a manifest for your project. It keeps track of your project's name, version, and the libraries you install.