Repository navigation
Expand file tree
/
Copy pathnode3.txt
More file actions
92 lines (48 loc) · 1.49 KB
/
Copy pathnode3.txt
File metadata and controls
92 lines (48 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Chapter 3 – Node.js Modules & File System
1. What Are Modules in Node.js?
Modules are separate files or features that you can include in your Node.js program.
They help break large programs into smaller, reusable parts.
Node.js has 2 types of modules:
Built-in Modules → already available in Node.js
Custom Modules → created by you
External Modules → installed using npm
2. Built-in Modules in Node.js
Some commonly used built-in modules:
fs → File System
http → Create server
path → Work with file paths
os → Operating system details
To use a module:
const fs = require("fs");
3. File System Module (fs)
This module allows you to:
Create files
Read files
Update files
Delete files
Rename files
Examples of FS operations:
fs.writeFile()
fs.appendFile()
fs.readFile()
fs.unlink()
fs.rename()
4. Synchronous vs Asynchronous
Node.js operations are mostly asynchronous (non-blocking).
But every FS operation has a sync version:
Examples:
readFile() → async
readFileSync() → sync
Async is preferred for performance.
5. Creating Your Own Module
Any .js file becomes a module.
You can export functions using:
module.exports = functionName;
And import using:
const fn = require("./myModule");
6. Summary of This Chapter
Modules allow organized coding
Use built-in modules like fs, http, os, path
FS module helps in reading/writing files
Difference between sync & async functions
Custom modules help reuse your own code