Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
dddeb35
Rename 1-intro.js to 01-intro.js
boubkhaled Apr 5, 2021
17e7fe4
Rename 2-globals.js to 02-globals.js
boubkhaled Apr 5, 2021
ca4ed55
Rename 3-modules.js to 03-modules.js
boubkhaled Apr 5, 2021
a4c291b
Rename 4-names.js to 04-names.js
boubkhaled Apr 5, 2021
fc4dc61
Rename 5-utils.js to 05-utils.js
boubkhaled Apr 5, 2021
337f59d
Rename 6-alternative-flavor.js to 06-alternative-flavor.js
boubkhaled Apr 5, 2021
1cf5e27
Rename 7-mind-grenade.js to 07-mind-grenade.js
boubkhaled Apr 5, 2021
bf5b767
Rename 8-os-module.js to 08-os-module.js
boubkhaled Apr 5, 2021
e7063cb
Rename 9-path-module.js to 09-path-module.js
boubkhaled Apr 5, 2021
2ffab1e
Merge branch 'john-smilga:main' into main
boubkhaled Sep 23, 2021
40be667
Merge branch 'john-smilga:main' into main
boubkhaled Feb 15, 2026
f7f6cb8
Clean up introductory Node.js tutorial content
boubkhaled Feb 15, 2026
15537c0
Create tutorial on Node.js globals 02-globals.md
boubkhaled Feb 15, 2026
4c3bdee
Revise Node.js globals tutorial introduction
boubkhaled Feb 15, 2026
bbf37e9
Add Node.js Modules Tutorial using CommonJS
boubkhaled Feb 16, 2026
a5eb0cb
Add Node.js OS Module tutorial
boubkhaled Feb 16, 2026
306bd92
Delete 01-node-tutorial/os-module.md
boubkhaled Feb 16, 2026
f386606
Add tutorial for Node.js OS module usage
boubkhaled Feb 16, 2026
ed7a96c
Add Node.js Path Module Tutorial
boubkhaled Feb 16, 2026
d7c2718
Create 10-fs-sync.md
boubkhaled Feb 16, 2026
25ecde5
Create 11-fs-async.md
boubkhaled Feb 16, 2026
362c4e5
Create 12-http.md
boubkhaled Feb 16, 2026
3555602
Create 13-event-emitter.md
boubkhaled Feb 16, 2026
df0f0ea
Create 15-create-big-file.md
boubkhaled Feb 16, 2026
4eb2002
Create 16-streams.md
boubkhaled Feb 16, 2026
99e1cdb
Create 17-http-stream.md
boubkhaled Feb 16, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 215 additions & 0 deletions 01-node-tutorial/01-intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
## Introduction

This tutorial guides you through your first Node.js application. Node.js allows you to run JavaScript outside of a web browser, on servers and command-line tools.

## What is Node.js?

Node.js is a runtime environment that lets you execute JavaScript code on the server-side. It's built on top of Chrome's V8 JavaScript engine and is perfect for building scalable applications.

## Your First Node.js App

Let's examine the code from `01-intro.js`:

```javascript
const amount = 9

if (amount < 10) {
console.log('small number')
} else {
console.log('large number')
}

console.log(`hey it's my first node app!!!`)
```

### Breaking Down the Code

#### 1. **Variable Declaration**

```javascript
const amount = 9
```

- **`const`**: Declares a constant variable that cannot be reassigned after initialization
- **`amount`**: The variable name
- **`9`**: The initial value (a number)

**Key Points:**
- Use `const` by default (it's safer than `let` or `var`)
- Use `let` if you need to reassign the variable
- Avoid `var` in modern JavaScript

#### 2. **Conditional Logic (if/else statement)**

```javascript
if (amount < 10) {
console.log('small number')
} else {
console.log('large number')
}
```

**What happens here:**
- The `if` statement checks if `amount` is less than 10
- If the condition is `true`, it executes the code in the first block
- Otherwise (when condition is `false`), it executes the `else` block

**In this example:**
- Since `amount = 9`, and `9 < 10` is `true`
- The output will be: `small number`

#### 3. **Template Literals (String Interpolation)**

```javascript
console.log(`hey it's my first node app!!!`)
```

**Key Features:**
- Uses backticks (`` ` ``) instead of quotes
- Allows you to embed variables using `${}` syntax
- Makes multi-line strings easier to work with

**Example with variable interpolation:**
```javascript
const name = "Node.js"
console.log(`Welcome to ${name}!`) // Output: Welcome to Node.js!
```

#### 4. **Console Output**

```javascript
console.log()
```

- Prints messages to the terminal/console
- Essential for debugging and monitoring your application

## Running the Code

### Prerequisites
- Node.js installed on your computer
- A terminal or command prompt

### Steps

1. **Create a file** named `01-intro.js` with the code above

2. **Open your terminal** and navigate to the file's directory:
```bash
cd path/to/your/directory
```

3. **Run the file** using Node.js:
```bash
node 01-intro.js
```

4. **Expected Output:**
```
small number
hey it's my first node app!!!
```

## Key Concepts Learned

| Concept | Explanation |
|---------|-------------|
| **const** | Declares an immutable variable |
| **if/else** | Conditional statement for decision-making |
| **Comparison Operator (<)** | Checks if a value is less than another |
| **console.log()** | Prints output to the console |
| **Template Literals** | String literals that allow embedded expressions |

## Practice Exercises

### Exercise 1: Change the Condition
Modify the code to check if `amount` is greater than 10:

```javascript
const amount = 9

if (amount > 10) {
console.log('large number')
} else {
console.log('small number')
}
```

**Question:** What will be the output?

### Exercise 2: Use Different Operators
Try these comparison operators:
- `>` (greater than)
- `>=` (greater than or equal)
- `<=` (less than or equal)
- `===` (strictly equal)
- `!==` (not equal)

### Exercise 3: Multiple Conditions with else if
```javascript
const amount = 10

if (amount < 10) {
console.log('small number')
} else if (amount === 10) {
console.log('exactly ten')
} else {
console.log('large number')
}
```

### Exercise 4: Using Variables in Template Literals
```javascript
const amount = 9

if (amount < 10) {
console.log(`${amount} is a small number`)
} else {
console.log(`${amount} is a large number`)
}
```

## Next Steps

Now that you understand the basics, explore:

1. **Loops** - Repeat code multiple times
2. **Functions** - Create reusable code blocks
3. **Objects & Arrays** - Store and manage data
4. **File System** - Read and write files
5. **Modules** - Import and export code
6. **npm Packages** - Use third-party libraries

## Common Mistakes to Avoid

❌ **Using `var` instead of `const`/`let`**
```javascript
var amount = 9 // Avoid this
const amount = 9 // Better
```

❌ **Forgetting quotes in console.log()**
```javascript
console.log(hey it's my first node app) // Error!
console.log('hey it\'s my first node app') // Correct
console.log(`hey it's my first node app`) // Best (template literal)
```

❌ **Using = instead of === for comparison**
```javascript
if (amount = 10) { } // Wrong! This assigns, not compares
if (amount === 10) { } // Correct! Compares values
```

## Summary

Congratulations! You've created your first Node.js application. You've learned about:
- ✅ Variable declaration with `const`
- ✅ Conditional logic with `if/else`
- ✅ String output with `console.log()`
- ✅ Template literals for dynamic strings

Continue building on these fundamentals to master Node.js!
```

This tutorial provides a comprehensive guide to your first Node.js application, covering the concepts, execution steps, and practical exercises to reinforce learning.
151 changes: 151 additions & 0 deletions 01-node-tutorial/02-globals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
## Overview

In Node.js, there are global objects and variables that are available in every module without needing to import them. Unlike browser JavaScript which has the `window` object, Node.js provides its own set of globals designed for server-side development.

## Key Node.js Globals

### 1. **`__dirname`** - Current Directory Path
```javascript
console.log(__dirname)
```

**What it does:** Returns the absolute path of the current directory containing the currently executing file.

**Example output:**
```
/Users/username/projects/node-express-course/01-node-tutorial
```

**Common use cases:**
- Reading files relative to the current directory
- Setting up file paths for asset serving
- Constructing dynamic paths for logging

**Example:**
```javascript
const path = require('path');
const filePath = path.join(__dirname, 'data', 'users.json');
```

---

### 2. **`__filename`** - Current File Name
**What it does:** Returns the absolute file path of the currently executing file.

**Example output:**
```
/Users/username/projects/node-express-course/01-node-tutorial/02-globals.js
```

---

### 3. **`require()`** - Module System Function
**What it does:** Allows you to import modules using the CommonJS module system.

**Example:**
```javascript
const express = require('express');
const fs = require('fs');
const path = require('path');
```

---

### 4. **`module`** - Current Module Information
**What it does:** Contains information about the current module, including exports and other metadata.

**Example:**
```javascript
console.log(module)
// Shows: { exports: {}, ...other properties }

module.exports = { name: 'My Module' };
```

---

### 5. **`process`** - Environment Information
**What it does:** Provides information about the Node.js process and environment variables.

**Common properties:**
```javascript
process.env // Environment variables
process.argv // Command-line arguments
process.cwd() // Current working directory
process.exit() // Exit the process
process.version // Node.js version
```

**Example:**
```javascript
console.log(process.env.NODE_ENV); // 'development' or 'production'
console.log(process.argv); // Array of command-line arguments
```

---

## Practical Example: setInterval

The code also demonstrates **`setInterval()`**, a global function for running code repeatedly:

```javascript
setInterval(() => {
console.log('hello world')
}, 1000)
```

**What it does:** Runs the callback function every 1000 milliseconds (1 second).

**Output:**
```
hello world
hello world
hello world
... (repeats indefinitely)
```

---

## Complete Working Example

```javascript
// Display current directory and file
console.log('Current directory:', __dirname);
console.log('Current file:', __filename);

// Run a function every 1 second
let count = 0;
setInterval(() => {
count++;
console.log(`[${count}] hello world`);

// Stop after 5 iterations
if (count === 5) {
process.exit(0); // Use process.exit() to terminate
}
}, 1000);
```

---

## Key Differences from Browser JavaScript

| Browser | Node.js |
|---------|---------|
| `window` global | `global` global |
| `document` available | No DOM |
| Can't access file system directly | `fs` module for file access |
| No `__dirname` or `__filename` | Both available automatically |
| `process` not available | `process` always available |

---

## Summary

Node.js globals are essential for:
- ✅ Working with file paths (`__dirname`, `__filename`)
- ✅ Managing modules (`require`, `module`)
- ✅ Accessing environment configuration (`process`)
- ✅ Creating timers and intervals (`setInterval`, `setTimeout`)

These globals make it easy to build server-side applications without needing to import everything explicitly!
Loading