Here’s a focused preparation guide for Unit II: Node.
js topics for your exam tomorrow:
1. Basics of Node.js
What is Node.js?
A JavaScript runtime environment for server-side programming.
Built on Chrome’s V8 JavaScript engine.
Non-blocking, event-driven architecture.
Environment Setup:
Install Node.js from the official site Node.js.
Verify installation:
Node -v
Npm -v
2. Events and Listeners
Events: Node.js follows an event-driven model using the EventEmitter class.
Example: Custom Event and Listener
Const EventEmitter = require(‘events’);
Const eventEmitter = new EventEmitter();
// Listener
eventEmitter.on(‘greet’, () => {
console.log(‘Hello, Event Triggered!’);
});
// Trigger the event
eventEmitter.emit(‘greet’);
3. Timers and Callbacks
Timers: setTimeout, setInterval, and setImmediate.
Example: Using Timers
setTimeout(() => console.log(‘Executed after 2 seconds’), 2000);
setInterval(() => console.log(‘Repeats every 1 second’), 1000);
Callbacks: Functions passed as arguments to be executed later.
Example: Callback with File System
Const fs = require(‘fs’);
Fs.readFile(‘example.txt’, ‘utf8’, (err, data) => {
If (err) throw err;
Console.log(data);
});
4. Handling Data I/O & File System
Reading and Writing Files
Reading Files
Const fs = require(‘fs’);
Fs.readFile(‘example.txt’, ‘utf8’, (err, data) => {
If (err) throw err;
Console.log(data);
});
Writing Files
Const fs = require(‘fs’);
Fs.writeFile(‘example.txt’, ‘Hello, Node.js!’, (err) => {
If (err) throw err;
Console.log(‘File written successfully!’);
});
File System Tasks
Renaming, deleting, creating directories using the fs module.
Example: Deleting a file
Fs.unlink(‘example.txt’, (err) => {
If (err) throw err;
Console.log(‘File deleted!’);
});
5. Socket Services in Node.js
Socket Services: Enable real-time communication using net or external libraries like
Socket.IO.
Example: TCP Server
Const net = require(‘net’);
Const server = net.createServer((socket) => {
Console.log(‘Client connected!’);
Socket.on(‘data’, (data) => {
Console.log(`Received: ${data}`);
Socket.write(‘Hello from server!’);
});
});
Server.listen(8080, () => {
Console.log(‘Server listening on port 8080’);
});
Example: TCP Client
Const net = require(‘net’);
Const client = net.createConnection({ port: 8080 }, () => {
Console.log(‘Connected to server!’);
Client.write(‘Hello, Server!’);
});
Client.on(‘data’, (data) => {
Console.log(`Server says: ${data}`);
Client.end();
});
6. Key Focus Areas for Exam
Understand Asynchronous Nature: Node.js uses non-blocking I/O.
Practice File System Operations: Read, write, delete, and rename files.
Socket Programming: Know how to create TCP servers and clients.
Timers: Know how to use setTimeout and setInterval.
Error Handling: Always handle errors in callbacks (e.g., if (err) throw err).
Revision Tips
1. Write and run small code snippets for each topic.
2. Use comments to explain your code during the exam for better clarity.
3. Revise important methods in the fs and net modules.
4. Rest well and stay confident!
Let me know if you need further clarification or additional examples!