Lesson 1 of 50 – Node.js Introduction
2%
2%

Introduction to Node.js

Node.js is an open-source, cross-platform JavaScript runtime environment that allows us to execute JavaScript outside a web browser.

We can use Node.js to create web servers, backend applications, REST APIs, command-line tools, and real-time applications.

Remember: Node.js is not a programming language. JavaScript is the programming language, and Node.js allows JavaScript to run outside the browser.

What Is Node.js?

Normally, JavaScript runs inside web browsers such as Google Chrome. Node.js uses Chrome's V8 JavaScript engine to execute JavaScript programs on a computer or server.

JavaScript Runtime

Node.js provides an environment for executing JavaScript code without needing a web browser.

Backend Development

Node.js can handle server-side operations, process requests, communicate with databases, and send responses to clients.

Cross-Platform

Node.js can run on Windows, macOS, and Linux.

Features of Node.js

1. Asynchronous Programming

Node.js supports asynchronous operations. It can start an input/output operation and continue other work while waiting for that operation to finish.

2. Event-Driven

Node.js can respond to events such as incoming requests, completed operations, and user-defined events.

3. NPM

NPM (Node Package Manager) helps developers install and manage reusable packages for their projects.

4. Built-in Modules

Node.js provides built-in modules such as fs, path, http, and os for working with files, paths, servers, and operating-system information.

JavaScript in Browser vs Node.js

Comparison
JavaScript in Browser JavaScript in Node.js
Runs inside a web browser. Runs outside the browser.
Can manipulate webpage HTML using the DOM. Can access Node.js APIs such as the file system.
Commonly used for frontend development. Commonly used for backend development.
Uses browser APIs such as document. Uses APIs such as fs and http.

What Is NPM?

NPM stands for Node Package Manager. It is commonly used to install, update, and manage packages in Node.js applications.

Example: Installing Express.js

Run the following command in your terminal:

npm install express

Express.js is a popular framework for creating web servers and APIs with Node.js.

Checking Node.js Installation

After installing Node.js, open Command Prompt or a terminal and enter the following commands.

Check Node.js Version
node -v

This command displays the installed Node.js version.

Check NPM Version
npm -v

This command displays the installed NPM version.

Our First Node.js Program

Let's create a simple program that displays a message in the terminal.

Step 1: Create a File

Create a file named app.js.

Step 2: Write the Code
app.js
console.log("Hello, Node.js!");
Step 3: Run the Program

Open the terminal in the folder containing the file and run:

node app.js
Output
Terminal Output
Hello, Node.js!
Explanation
  • console.log() displays information in the terminal.
  • "Hello, Node.js!" is the message to display.
  • node app.js executes the JavaScript file using Node.js.

Example: Display Student Details

We can also use JavaScript variables in Node.js.

Student Details Program
let studentName = "Rahul";
let studentAge = 20;
let course = "Node.js";

console.log("Student Name:", studentName);
console.log("Student Age:", studentAge);
console.log("Course:", course);
Output
Student Name: Rahul
Student Age: 20
Course: Node.js

Where Is Node.js Used?

Web Applications
Used to develop backend services for websites and web applications.
REST APIs
Used to receive requests and send data to frontend applications.
Chat Applications
Can support real-time messaging and communication features.
Automation Tools
Can run scripts to automate repetitive tasks.

Advantages of Node.js

  • Uses JavaScript for backend development.
  • Provides many built-in modules.
  • Supports asynchronous input/output operations.
  • Has a large ecosystem of packages through NPM.
  • Runs on multiple operating systems.

Limitations of Node.js

  • CPU-intensive tasks can block the main event loop if not handled properly.
  • Asynchronous programming may be challenging for beginners.
  • Third-party packages must be selected and maintained carefully.
💡 Remember
JavaScript is the programming language. Node.js provides the runtime environment, and NPM helps manage project packages.

Practical Assignment

Practice Tasks
  1. Install Node.js on your computer.
  2. Check the installed version using node -v.
  3. Check the NPM version using npm -v.
  4. Create a file named app.js.
  5. Display your name using console.log().
  6. Create variables for your name, age, and course.
  7. Display all three values in the terminal.

🧠 Quiz

1. What is Node.js?

2. Which JavaScript engine does Node.js use?

3. Which command runs app.js using Node.js?