Node.js Fundamentals Introduction
Learn the fundamental concepts of Node.js, understand how JavaScript executes in the Node.js runtime, work with modules, use built-in objects, and prepare to build backend applications.
1. Introduction to Node.js Fundamentals
Node.js fundamentals are the basic concepts required to understand how Node.js applications work. Before building APIs, web servers, or backend applications, we should understand how to execute JavaScript, use modules, access built-in objects, and interact with the runtime environment.
Node.js allows JavaScript to run outside the browser. It provides APIs for tasks such as working with files, accessing environment variables, handling process information, and creating network applications.
Topics Covered in Node.js Fundamentals
| Topic | Description | Example |
|---|---|---|
| Running JavaScript | Execute JavaScript files using Node.js. | node app.js |
| Global Objects | Use objects and functions available in the Node.js environment. | console.log() |
| Process Object | Access information about the current Node.js process. | process.version |
| Command-Line Arguments | Pass input values when starting an application. | process.argv |
| Environment Variables | Read configuration values from the environment. | process.env |
2. How Node.js Executes JavaScript
Node.js uses the V8 JavaScript engine to execute JavaScript. When we run a JavaScript file, Node.js loads the code and executes it using the runtime environment.
app.js
Node.js also provides APIs and runtime facilities that allow JavaScript applications to perform operations such as file access and network communication.
3. Write Your First Node.js Program
Let us begin with a simple program that prints messages in the terminal. This example demonstrates how JavaScript executes using Node.js.
Step 1: Create app.js
console.log("Welcome to Node.js!");
console.log("Learning Node.js Fundamentals.");
console.log("CIIT TRAINING INSTITUTE");
Step 2: Execute the Program
node app.js
Expected Output
Welcome to Node.js!
Learning Node.js Fundamentals.
CIIT TRAINING INSTITUTE
Code Explanation
console.log()prints information to the console.- Text inside quotation marks represents a string.
- Each statement prints a separate line.
node app.jstells Node.js to execute the file.
4. JavaScript and Node.js
JavaScript is the programming language. Node.js is one environment where JavaScript can execute. The same language can also execute in a browser, but the available APIs differ between environments.
| Feature | Browser JavaScript | Node.js JavaScript |
|---|---|---|
| Runtime | Web browser | Node.js runtime |
| HTML DOM | Usually available through browser APIs. | Not available by default. |
| File System | Restricted browser APIs. | Can use Node.js file-system APIs. |
| Backend Servers | Not the browser's normal role. | Can create HTTP servers and backend services. |
| Environment Variables | Not available as Node.js process variables. | Can be accessed through process.env. |
5. Important Node.js Capabilities
1. JavaScript Execution
Execute JavaScript files directly from the terminal.
2. File System Access
Read, write, and manage files using the
fs module.
3. Module System
Organize code into reusable modules and import functionality when required.
4. Process Information
Inspect the runtime version, arguments, and environment configuration.
5. Network Applications
Build HTTP servers and network-based applications.
6. Package Management
Use npm to install and manage third-party packages.
6. Common Node.js Commands
These commands are useful when working with Node.js projects from the terminal.
| Command | Purpose |
|---|---|
node --version |
Displays the Node.js version. |
npm --version |
Displays the npm version. |
node |
Opens the interactive REPL when no file is specified. |
node app.js |
Executes the JavaScript file named app.js. |
npm init -y |
Creates a default package.json file. |
npm install |
Installs project dependencies described by the package configuration and lockfile when available. |
7. Practical Example: Calculate Student Marks
Let us create a small program that calculates the total and average marks of a student. This example uses basic JavaScript statements executed by Node.js.
Step 1: Create student.js
const studentName = "Student";
const subject1 = 80;
const subject2 = 75;
const subject3 = 90;
const total = subject1 + subject2 + subject3;
const average = total / 3;
console.log("Student:", studentName);
console.log("Total Marks:", total);
console.log("Average Marks:", average);
Step 2: Run the Program
node student.js
Expected Output
Student: Student
Total Marks: 245
Average Marks: 81.66666666666667
Explanation
constdeclares variables whose bindings cannot be reassigned.- The total is calculated by adding the three subject marks.
- The average is calculated by dividing the total by three.
console.log()displays the calculated results.
8. Best Practices for Beginners
- Use meaningful variable and function names.
- Keep application code organized into separate files and modules.
- Read terminal error messages carefully.
- Use npm to manage project dependencies.
- Keep configuration values separate from application logic.
- Avoid placing passwords, tokens, or other secrets directly in source code.
- Practice each concept with small JavaScript programs before building larger applications.
9. CIIT Training Institute - Practice Task
Task: Create a Course Information Program
Create a file named course.js and complete
the following activities:
- Create a variable to store the institute name.
- Create a variable to store a course name.
- Store the course duration in months.
- Calculate the total fee for two course installments.
- Print all the information using
console.log(). - Execute the file using
node course.js.
Sample Program
const institute = "CIIT TRAINING INSTITUTE";
const course = "Node.js";
const duration = 3;
const installment1 = 10000;
const installment2 = 10000;
const totalFee = installment1 + installment2;
console.log("Institute:", institute);
console.log("Course:", course);
console.log("Duration:", duration, "months");
console.log("Total Fee:", totalFee);
Expected Output
Institute: CIIT TRAINING INSTITUTE
Course: Node.js
Duration: 3 months
Total Fee: 20000
Summary
Node.js fundamentals explain how JavaScript runs outside the browser and how the Node.js runtime provides APIs for application development. We learned about JavaScript execution, the runtime and V8 engine, common terminal commands, built-in capabilities, and practical calculations. Understanding these concepts prepares students at CIIT TRAINING INSTITUTE to continue learning JavaScript execution, global objects, process information, command-line arguments, and environment variables.