-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnode_tutorial copy.html
More file actions
386 lines (340 loc) · 12.9 KB
/
node_tutorial copy.html
File metadata and controls
386 lines (340 loc) · 12.9 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Node.js Tutorial</title>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<style>
/* General Styles */
body {
font-family: 'Arial', sans-serif;
background: #f4f4f4;
margin: 0;
padding: 0;
transition: 0.3s;
}
.dark-mode {
background: #222;
color: #fff;
}
/* Header */
header {
background: #2c3e50;
color: #fff;
padding: 10px 20px;
display: flex;
justify-content: space-between;
align-items: center;
position: sticky;
top: 0;
z-index: 1000;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
.logo {
font-size: 24px;
font-weight: bold;
color: #fff;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
align-items: center;
}
nav ul li {
margin-left: 25px;
}
nav ul li a {
color: #fff;
text-decoration: none;
font-weight: bold;
font-size: 16px;
transition: color 0.3s ease;
}
nav ul li a:hover {
color: #ff6f61;
}
.hamburger {
display: none;
font-size: 24px;
cursor: pointer;
}
/* Main Content */
.container {
max-width: 900px;
margin: 20px auto;
padding: 20px;
background: #fff;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.dark-mode .container {
background: #333;
box-shadow: 0 4px 8px rgba(255, 255, 255, 0.2);
}
h1 {
text-align: center;
color: #007bff;
}
.search-box {
padding: 10px;
width: 80%;
max-width: 400px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
margin: 20px auto;
display: block;
}
.toggle-btn {
background: #008CBA;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
margin: 10px auto;
display: block;
transition: background 0.3s ease;
}
.toggle-btn:hover {
background: #005f73;
}
/* Sections */
.section {
background: #f9f9f9;
padding: 15px;
margin: 10px 0;
border-radius: 10px;
cursor: pointer;
border-left: 5px solid #007bff;
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.section:hover {
transform: translateY(-5px);
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}
.content {
display: none;
padding: 15px;
background: #fff;
border-left: 5px solid #007bff;
color: black;
margin: 10px 0;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.dark-mode .content {
background: #444;
color: white;
border-left: 5px solid #008CBA;
}
pre {
background: #1e1e1e;
color: lime;
padding: 15px;
border-radius: 5px;
overflow-x: auto;
position: relative;
}
.copy-btn {
position: absolute;
top: 10px;
right: 10px;
background: #007bff;
color: white;
border: none;
padding: 5px 10px;
border-radius: 3px;
cursor: pointer;
transition: background 0.3s ease;
}
.copy-btn:hover {
background: #0056b3;
}
textarea {
width: 100%;
height: 100px;
font-family: monospace;
margin-top: 10px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 14px;
}
.run-btn {
background: #28a745;
color: white;
padding: 8px 16px;
border: none;
border-radius: 5px;
cursor: pointer;
margin-right: 10px;
transition: background 0.3s ease;
}
.run-btn:hover {
background: #218838;
}
a {
display: block;
text-align: center;
margin-top: 20px;
padding: 10px;
color: white;
text-decoration: none;
border-radius: 5px;
transition: background 0.3s ease;
}
a:hover {
background: #0056b3;
}
/* Footer */
footer {
background: #1a252f;
color: #fff;
text-align: center;
padding: 20px;
margin-top: 20px;
}
footer p {
margin: 0;
}
/* Responsive Design */
@media (max-width: 768px) {
.hamburger {
display: block;
}
nav ul {
display: none;
flex-direction: column;
width: 100%;
background: #2c3e50;
position: absolute;
top: 60px;
left: 0;
}
nav ul.active {
display: flex;
}
nav ul li {
margin: 10px 0;
text-align: center;
}
.header h3 {
font-size: 24px;
}
.search-box {
width: 90%;
}
}
</style>
</head>
<body>
<!-- Header -->
<header>
<div class="logo">Learn Coding</div>
<nav>
<ul class="menu">
<li><a href="index.html">Home</a></li>
<li><a href="javascript_tutorial.html">JavaScript Tutorial</a></li>
<li><a href="#tutorials">Tutorials</a></li>
<li><a href="#topics">Topics</a></li>
<li><a href="#testimonials">Testimonials</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
<div class="hamburger" onclick="toggleMenu()">☰</div>
</nav>
</header>
<!-- Main Content -->
<div class="container">
<h1>📌 Node.js Tutorial - Questions & Answers</h1>
<input type="text" id="search" class="search-box" placeholder="🔍 Search topics..." onkeyup="searchTopics()">
<button class="toggle-btn" onclick="toggleDarkMode()">🌙 Toggle Dark Mode</button>
<!-- Sections -->
<div id="topics-container">
<!-- Questions will be dynamically added here -->
</div>
</div>
<!-- Footer -->
<footer id="contact">
<p>© 2023 Learn Coding. All rights reserved.</p>
</footer>
<script>
const topics = [
["What is Node.js?", "Node.js is an **open-source, cross-platform runtime** that allows JavaScript to run on the server-side."],
["How to install Node.js?", "Download the latest version from the official website and follow the installation steps."],
["What is npm in Node.js?", "npm (Node Package Manager) is used to manage JavaScript packages."],
["How to create a basic server in Node.js?", "Use the **http module** to create a simple server."],
["What is Express.js?", "Express.js is a **fast and minimal web framework** for Node.js."],
["What is Middleware in Express?", "Middleware functions **process requests** before sending a response."],
["How to handle JSON data in Node.js?", "Use `express.json()` to parse JSON requests in Express."],
["How to connect Node.js with MongoDB?", "Use **Mongoose** to connect with MongoDB."],
["How to create a REST API in Node.js?", "Use **Express.js** to create a REST API endpoint."],
["How to use Environment Variables in Node.js?", "Use **dotenv** package to manage environment variables safely."],
["What is Asynchronous Programming in Node.js?", "Node.js uses an event-driven, non-blocking I/O model."],
["How to use Async/Await in Node.js?", "Async/Await simplifies asynchronous code handling."],
["What is WebSocket in Node.js?", "WebSocket is a communication protocol that enables real-time, bi-directional communication between a client and a server."],
["How does WebSocket work?", "WebSocket starts with an HTTP handshake and then upgrades to a persistent connection, allowing both the client and server to send and receive messages anytime."],
["Why use WebSocket?", "WebSockets reduce network latency, improve performance, and are ideal for real-time applications like chat, gaming, and live updates."],
["How to implement WebSocket in Node.js?", "You can use the 'ws' library in Node.js to create a WebSocket server and handle real-time communication."],
["Example WebSocket setup?", "Install 'ws' with 'npm install ws', then create a WebSocket server using the WebSocket package in Node.js."],
["How does the WebSocket client communicate?", "A WebSocket client connects using 'new WebSocket(url)', listens for events like 'open', 'message', and 'close', and sends messages using 'socket.send(data)'."]
["How to use JWT for Authentication?", "JSON Web Token (JWT) is used for secure authentication."],
["What is Redis and How to Use it in Node.js?", "Redis is an in-memory data store, often used for caching."],
["What is Event Emitter in Node.js?", "EventEmitter allows event-driven programming in Node.js."],
["How to Handle Errors in Node.js?", "Use try-catch or error handlers for better error management."],
["What is Nodemon and Why Use It?", "Nodemon auto-restarts the server when code changes."],
["What is the Difference Between Process.nextTick() and setImmediate()?", "Process.nextTick() runs before the next event loop phase, while setImmediate() executes in the next cycle."],
["How to Deploy a Node.js App?", "Use **PM2** for process management & **Nginx** for load balancing."]
];
const topicsContainer = document.getElementById('topics-container');
topics.forEach((topic, index) => {
topicsContainer.innerHTML += `
<div class="section" onclick="toggleContent('q${index + 1}')">
<h2>${index + 1}️⃣ ${topic[0]}</h2>
</div>
<div id="q${index + 1}" class="content">
<p>${topic[1]}</p>
<textarea id="code${index + 1}">${topic[1]}</textarea><br>
<button class="run-btn" onclick="runCode('code${index + 1}')">Run</button>
<button class="copy-btn" onclick="copyToClipboard('code${index + 1}')">Copy</button>
</div>
`;
});
function toggleContent(id) {
let content = document.getElementById(id);
content.style.display = (content.style.display === "none" || content.style.display === "") ? "block" : "none";
}
function toggleDarkMode() {
document.body.classList.toggle("dark-mode");
Swal.fire("🌙 Dark mode " + (document.body.classList.contains("dark-mode") ? "enabled" : "disabled"));
}
function searchTopics() {
let input = document.getElementById("search").value.toLowerCase();
let sections = document.getElementsByClassName("section");
for (let section of sections) {
if (section.textContent.toLowerCase().includes(input)) section.style.display = "";
else section.style.display = "none";
}
}
function copyToClipboard(codeId) {
let codeElement = document.getElementById(codeId);
codeElement.select();
document.execCommand("copy");
Swal.fire("✅ Code copied!");
}
function runCode(codeId) {
let code = document.getElementById(codeId).value;
try { eval(code); }
catch (error) { Swal.fire("❌ Error: " + error.message); }
}
function toggleMenu() {
const menu = document.querySelector('.menu');
menu.classList.toggle('active');
}
</script>
</body>
</html>