Date: 18/02/25
PRNNO: 22310222
Roll No: 291010
Name : Aditya Sahu
Assignment Number: 04
Assignment Title / Problem Statement / Objective:
Understand event-driven programming in Node.js by creating a simple chat application.
& Node.js File System and Asynchronous Programming
Brief procedure or steps / subtasks to be implemented
Create a Node.js script that:
1. Creates a simple chat application using the events module.
2. Emits events for different chat messages (e.g., "message", "join", "leave").
3. Listens for these events and prints corresponding messages to the console.
4. Allows users to input chat messages and emit corresponding events.
1. Reads the contents of a directory.
2. Prints the names of all files and subdirectories in the console.
3. Creates a new file named "example.txt".
4. Appends a message to the newly created file.
Key concepts to be learnt or theory
These assignments introduce key backend concepts using Node.js. Students learn how
to create basic
HTTP servers, serve static files, and handle routing. They also understand how to
implement error
handling (like 404 responses) and use Node's built-in fs module to read/write files.
Logging requests
helps build awareness of server-side monitoring and debugging practices.
Lab Assignment No 4 : Program Code
for
basic node server : console.log("\nType your message and press Enter.
Type 'exit' to leave.\n");
chat.js
const EventEmitter = require('events');
rl.on('line', (input) => {
const readline = require('readline');
if (input.trim().toLowerCase() === 'exit') {
class ChatApp extends EventEmitter {}
chat.emit('leave', username);
const chat = new ChatApp();
} else {
const rl = readline.createInterface({
chat.emit('message', username, input);
input: process.stdin,
}
output: process.stdout
});
});
});
chat.on('join', (username) => { Expo.js
console.log(` ${username} has joined the chat.`); const fs = require('fs');
}); const path = require('path');
chat.on('message', (username, message) => { const directoryPath = __dirname;
console.log(` ${username}: ${message}`);
fs.readdir(directoryPath, (err, files) => {
});
if (err) {
console.error("❌ Error reading directory:", err);
chat.on('leave', (username) => {
return;
console.log(` ${username} has left the chat.`);
}
rl.close();
});
console.log("\n📂 Files and Folders in Directory:");
files.forEach(file => console.log(" 📄", file));
rl.question("Enter your name: ", (username) => {
chat.emit('join', username);
const filePath = path.join(directoryPath, 'example.txt');
const fileListText = `📂 Directory Results / Output : Diagrams / Selected
Listing:\n${files.join('\n')}\n\n`;
Screen Shots
fs.writeFile(filePath, fileListText, (err) => {
if (err) {
console.error("❌ Error creating file:", err);
return;
console.log("\n✅ 'example.txt' has been created with
directory listing.");
fs.appendFile(filePath, 'This file contains the directory
structure.\n', (err) => {
if (err) {
console.error("❌ Error appending to file:", err);
return;
console.log("📝 Additional message has been added to
'example.txt'.");
});
});
});
Observations or Conclusions:
These Node.js assignments provided a solid
foundation in building and managing simple
web servers.
By serving static files and implementing
request logging, students gained practical
experience with core modules like http and fs,
and understood how to handle client requests,
responses, and server-side file operations
effectively.