Node.
js Programs - Full Syllabus (Programs 1 to 15)
Program 1: Build a Simple Web Server
const http = require('http');
http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write("<h1>Hello, Welcome to Node.js Web Server!</h1>");
res.end();
}).listen(3000);
console.log("Server running at http://localhost:3000/");
// Output: Open browser and visit http://localhost:3000
// You will see: Hello, Welcome to Node.js Web Server!
Program 2: Display Multiplication Table
const num = 5;
for (let i = 1; i <= 10; i++) {
console.log(`${num} x ${i} = ${num * i}`);
}
// Output:
// 5 x 1 = 5
// 5 x 2 = 10
// ...
Program 3: Calculator using Node.js module
// calculator_app.js
const readline = require('readline');
const calc = {
add: (a, b) => a + b,
sub: (a, b) => a - b,
mul: (a, b) => a * b,
div: (a, b) => b !== 0 ? a / b : "Cannot divide by zero"
};
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Enter first number: ", (num1) => {
rl.question("Enter second number: ", (num2) => {
rl.question("Choose operation (add, sub, mul, div): ", (op) => {
const a = parseFloat(num1);
const b = parseFloat(num2);
let result;
switch(op) {
case "add": result = calc.add(a, b); break;
case "sub": result = calc.sub(a, b); break;
case "mul": result = calc.mul(a, b); break;
case "div": result = calc.div(a, b); break;
Node.js Programs - Full Syllabus (Programs 1 to 15)
default: result = "Invalid operation";
}
console.log(`Result: ${result}`);
rl.close();
});
});
});
// Output:
// Enter first number: 5
// Enter second number: 3
// Choose operation (add, sub, mul, div): mul
// Result: 15
Program 4: Create a file with data
const fs = require('fs');
fs.writeFile('sample.txt', 'Welcome to Node.js File System!', (err) => {
if (err) throw err;
console.log('File created and data written!');
});
// Output:
// File created and data written!
Program 5: Read a file content
const fs = require('fs');
fs.readFile('sample.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log("File Content:\n" + data);
});
// Output:
// File Content:
// Welcome to Node.js File System!
Program 6: Delete a File
const fs = require('fs');
fs.unlink('sample.txt', (err) => {
if (err) {
console.log("File not found or already deleted.");
} else {
console.log("File deleted successfully.");
}
});
// Output:
// File deleted successfully.
Node.js Programs - Full Syllabus (Programs 1 to 15)
Program 7: Write content to sample.txt
const fs = require('fs');
const content = "Hello! This content was written using Node.js";
fs.writeFile('sample.txt', content, (err) => {
if (err) throw err;
console.log('Content written to sample.txt');
});
// Output:
// Content written to sample.txt
Program 8: Return array of lines from file
const fs = require('fs');
fs.readFile('sample.txt', 'utf8', (err, data) => {
if (err) throw err;
const lines = data.split('\n');
console.log("Lines in file as array:");
console.log(lines);
});
// Output:
// Lines in file as array:
// [ 'Hello!', 'This content was written using Node.js' ]
Program 9: Read HTML file and return content
const fs = require('fs');
fs.readFile('index.html', 'utf8', (err, data) => {
if (err) console.log("HTML file not found.");
else console.log("HTML File Content:\n", data);
});
// Output:
// HTML File Content:
// <html><body><h1>Welcome</h1></body></html>
Program 10: Create file using appendFile
const fs = require('fs');
fs.appendFile('log.txt', 'This is a new line added.\n', (err) => {
if (err) throw err;
console.log("Line appended to log.txt");
});
// Output:
// Line appended to log.txt
Node.js Programs - Full Syllabus (Programs 1 to 15)
Program 11: Open requested file and return content
const http = require('http');
const fs = require('fs');
const url = require('url');
http.createServer((req, res) => {
const q = url.parse(req.url, true);
const filename = "." + q.pathname;
fs.readFile(filename, (err, data) => {
if (err) {
res.writeHead(404, {'Content-Type': 'text/html'});
return res.end("404 File Not Found");
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(3000);
console.log("Server running at http://localhost:3000");
// Output:
// Visit http://localhost:3000/sample.txt to see file content in browser.
Program 12: Read college website file
const fs = require('fs');
fs.readFile('college.html', 'utf8', (err, data) => {
if (err) console.log("File not found.");
else console.log("College Website Content:\n", data);
});
// Output:
// College Website Content:
// <html><body><h1>College Info</h1></body></html>
Program 13: Read product demo HTML
const fs = require('fs');
fs.readFile('product.html', 'utf8', (err, data) => {
if (err) console.log("Product page not found.");
else console.log("Product HTML Content:\n", data);
});
// Output:
// Product HTML Content:
// <html><body><h2>Product Page</h2></body></html>
Program 14: Read Query String
Node.js Programs - Full Syllabus (Programs 1 to 15)
const http = require('http');
const url = require('url');
http.createServer((req, res) => {
const q = url.parse(req.url, true).query;
const name = q.name;
const age = q.age;
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(`Name: ${name}<br>Age: ${age}`);
res.end();
}).listen(3000);
console.log("Go to: http://localhost:3000/?name=Ram&age=20");
// Output:
// Name: Ram
// Age: 20
Program 15: Split Query String
const http = require('http');
const url = require('url');
http.createServer((req, res) => {
const q = url.parse(req.url, true);
const queryObj = q.query;
res.writeHead(200, {'Content-Type': 'text/plain'});
for (let key in queryObj) {
res.write(`${key}: ${queryObj[key]}\n`);
}
res.end();
}).listen(3000);
console.log("Try: http://localhost:3000/?lang=nodejs&level=easy");
// Output:
// lang: nodejs
// level: easy