WEB TECHNOLOGIES
ASSIGMENT -1
- Familiarisation with REPL. Execute the following Commands: .help, .save, .load
a = 10 ; > b = 20 ; > a + b > a – b >a * b
2. Write a Node.js code to print your address on the console. Save the code in a .js
f
ile and execute it in REPL mode and the command prompt.
console.log("Name : Om Solkar");
console.log("Address: ABC Colony");
console.log("City : Pune");
console.log("State : Maharashtra");
console.log("Country: India");
3. Write a Node.js code to print all multiples of 7 numbers in between 13 and 47
inclusive of both if they are multiples of 7.
for (let i = 13; i <= 47; i++) {
if (i % 7 === 0) {
console.log(i);
}
}
4. Write a Node.js code to solve the expression x2+2xy+y2 using functions. Execute in
REPL and Command prompt.
function calculate(x, y) {
return (x * x) + (2 * x * y) + (y * y);
}
let x = 3;
let y = 4;
let result = calculate(x, y);
console.log("Result of x^2 + 2xy + y^2 is:", result);
5. Create a HTTP server to display your basic information Name, Age and Address on
the client.
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(" Basic Information");
res.write("Name: Om Solkar ");
res.write("Age: 21 ");
res.write("Address: Pune, Maharashtra, India ");
res.end(); });
server.listen(3000, () => {
console.log("Server running at http://localhost:3000"); });
6. Create a module that will display the current date and time along with a
concatenated message, once the client tries to access the HTTP server. (add both js
file in one folder)
File – time.js
const http = require('http');
const myModule = require('./mymodule');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('Node.js Module Demo');
res.write(myModule.getDateTimeMessage());
res.end(); });
server.listen(3000, () => {
console.log('Server running at http://localhost:3000'); });
file – module.js
exports.getDateTimeMessage = function () {
const now = new Date();
return "Hello Client! Current Date and Time is: " + now;
};
7. Write a Node.js code to accept a query string from the user on the server through
the URL, and write it back to the client.
const http = require('http');
const url = require('url');
const server = http.createServer((req, res) => {
const queryData = url.parse(req.url, true).query;
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write("Query String Data");
res.write("Name: " + queryData.name + " ");
res.write("Age: " + queryData.age + " ");
res.end(); });
server.listen(3000, () => {
console.log("Server running at http://localhost:3000"); });
8. Write a Node.Js code to pass specific input variables through the URL to the server
and the server should process them and respond back.
const http = require('http');
const url = require('url');
const server = http.createServer((req, res) => {
const query = url.parse(req.url, true).query;
const num1 = parseInt(query.num1);
const num2 = parseInt(query.num2);
const sum = num1 + num2;
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write("Processed Result");
res.write("Number 1: " + num1 + " ");
res.write("Number 2: " + num2 + " ");
res.write("Sum: " + sum + " ");
res.end(); });
server.listen(3000, () => {
console.log("Server running at http://localhost:3000"); });
9. Write a Node.js code to display your short Bio-Data (along with Photograph) saved
in an HTML file in response to the client’s access request to the server.
const http = require('http');
const fs = require('fs');
const path = require('path');
const server = http.createServer((req, res) => {
if (req.url === "/" || req.url === "/biodata.html") {
fs.readFile("biodata.html", (err, data) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(data);
res.end();
});
}
else if (req.url === "/photo.jpg") {
fs.readFile("photo.jpg", (err, data) => {
res.writeHead(200, { 'Content-Type': 'image/jpeg' });
res.write(data);
res.end();
});
}
});
server.listen(3000, () => {
console.log("Server running at http://localhost:3000");});
ASSIGNMENT – 2
Write a Node.js code which can create a file “Trial1.txt”. Insert some text in the file and
read the same from the file to be displayed.
const fs = require('fs');
const text = "This is Trial1.txt file.\nNode.js File System Module Demo.";
fs.writeFileSync("Trial1.txt", text);
const data = fs.readFileSync("Trial1.txt", "utf8");
console.log("File Content:");
console.log(data);
Write a Node.JS code to open an existing file and append the contents of the file. Post
updating the file display the contents of the file on the console.
const fs = require('fs');
const filePath = 'example.txt';
const contentToAppend = '\nThis is the new content being added.';
fs.appendFile(filePath, contentToAppend, (err) => {
if (err) {
console.error('Error appending to file:', err);
return;
}
console.log('Content appended successfully!\n');
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
console.log('Updated file content:\n');
console.log(data);
});
});
Write a Node.JS code to display the contents (text) of the file on the client side using HTTP
server
const http = require('http');
const fs = require('fs');
const path = require('path');
const filePath = path.join(__dirname, 'example.txt');
const server = http.createServer((req, res) => {
if (req.url === '/') {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Error reading file');
return;
}
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(data);
});
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Page not found');
}
});
server.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
Write a Node.JS code to display on demand the contents of two files (HTML Code) to the
client. Use HTTP server. ( make two html files – file1.html and file2.html )
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 3000;
const server = http.createServer((req, res) => {
if (req.url === '/' || req.url === '/file1') {
const filePath = path.join(__dirname, 'file1.html');
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('Error reading file1.html');
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(data); } });
} else if (req.url === '/file2') {
const filePath = path.join(__dirname, 'file2.html');
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('Error reading file2.html');
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(data); } });
} else {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('Page not found'); } });
server.listen(PORT, () => {
console.log(Server running at http://localhost:${PORT}); });
Write a Node.JS code to read data from a file and write it into some other file. Display
contents of both the files.
Inside the folder, create: input.txt → the source file, output.txt → this will be created by
Node.js , app.js → Node.js script
Input file –
Hello! This is the original content in input.txt.
Node.js will read this and write it into another file.
App.js
const fs = require('fs');
const inputFile = 'input.txt';
const outputFile = 'output.txt';
fs.readFile(inputFile, 'utf8', (err, data) => {
if (err) {
console.log('Error reading input file:', err);
return; }
console.log('Contents of input.txt:');
console.log(data);
console.log('----------------------------------');
fs.writeFile(outputFile, data, (err) => {
if (err) {
console.log('Error writing to output file:', err);
return; }
console.log('Data successfully written to output.txt');
fs.readFile(outputFile, 'utf8', (err, outputData) => {
if (err) {
console.log('Error reading output file:', err);
return; }
console.log('Contents of output.txt:');
console.log(outputData);
}); }); });
Write a Node.JS code to delete any text file.
Inside the folder, create a text file to delete, e.g., deleteMe.txt.
deleteMe.txt –
This file will be deleted by Node.js script.
Deletefile.js
const fs = require('fs');
const fileToDelete = 'deleteMe.txt';
fs.access(fileToDelete, fs.constants.F_OK, (err) => {
if (err) {
console.log(File "${fileToDelete}" does not exist.);
return;
}
fs.unlink(fileToDelete, (err) => {
if (err) {
console.log('Error deleting the file:', err);
return;
}
console.log(File "${fileToDelete}" has been deleted successfully.);
});
});
ASSIGMENT – 3
1 . Write a Node.Js code to create a Database HR and establish a connection.
const mysql = require('mysql');
const con = mysql.createConnection({
host: "localhost",
user: "root",
password: "" });
con.connect(function(err) {
if (err) {
console.log("Connection failed");
return; }
console.log("Connected to MySQL");
con.query("CREATE DATABASE HR", function (err, result) {
if (err) {
console.log("Database already exists or error occurred");
} else {
console.log("Database HR created successfully"); } }); });
2. Write a Node.Js code to Create tables with the following dependencies in the
database created in program 15. Insert appropriate records in the tables
const mysql = require('mysql');
const con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "HR"
});
con.connect((err) => {
if (err) {
console.log("Connection failed");
return; }
console.log("Connected to HR Database");
const createDept = CREATE TABLE IF NOT EXISTS DEPT ( DEPTNO INT PRIMARY KEY, DNAME VARCHAR(20), LOC VARCHAR(20) ) ;
con.query(createDept, (err) => {
if (err) throw err;
console.log("DEPT table created")
const insertDept = INSERT INTO DEPT VALUES (10,'ACCOUNTING','NEW YORK'), (20,'RESEARCH','DALLAS'), (30,'SALES','CHICAGO'), (40,'OPERATIONS','BOSTON');
con.query(insertDept, (err) => {
if (err) console.log("DEPT records may already exist");
else console.log("DEPT records inserted");
});
});
const createEmp = CREATE TABLE IF NOT EXISTS EMP ( EMPNO INT PRIMARY KEY, ENAME VARCHAR(20), JOB VARCHAR(20), MGR INT, HIREDATE DATE, SAL INT, COMM INT, DEPTNO INT, FOREIGN KEY (DEPTNO) REFERENCES DEPT(DEPTNO) );
con.query(createEmp, (err) => {
if (err) throw err;
console.log("EMP table created");
const insertEmp = INSERT INTO EMP VALUES (7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20), (7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30), (7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30), (7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20), (7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30), (7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10), (7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10) ;
con.query(insertEmp, (err) => {
if (err) console.log("EMP records may already exist");
else console.log("EMP records inserted");
});
});
});
-
- Write a Node.Js code to get the relevant data from the above table/s - List all employees who have a salary greater than 1000 but less
than 2400. - List department numbers and names in department name order. - Display all the different job types available in the organization
database. - List the details of the employees in departments 20 and 30 in alphabetical
order of names. - Display all employees names which have MI or LA in them - Display each employee’s name and hire-date from dept. 10 - Change the salary of all employees of department 10 such that the salary is
reduced by Rs.100/-
const mysql = require('mysql');
const con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "HR"});
con.connect((err) => {
if (err) {
console.log("Connection failed");
return; }
console.log("Connected to HR Database\n");
con.query(
"SELECT * FROM EMP WHERE SAL > 1000 AND SAL < 2400",
(err, result) => {
console.log("1. Employees with salary between 1000 and 2400:");
console.table(result); } );
con.query(
"SELECT DEPTNO, DNAME FROM DEPT ORDER BY DNAME",
(err, result) => {
console.log("\n2. Departments ordered by name:");
console.table(result); } );
con.query(
"SELECT DISTINCT JOB FROM EMP",
(err, result) => {
console.log("\n3. Different job types:");
console.table(result); } );
con.query(
"SELECT * FROM EMP WHERE DEPTNO IN (20,30) ORDER BY ENAME",
(err, result) => {
console.log("\n4. Employees of Dept 20 and 30 (Alphabetical):");
console.table(result); } );
con.query(
"SELECT ENAME FROM EMP WHERE ENAME LIKE '%MI%' OR ENAME LIKE '%LA%'",
(err, result) => {
console.log("\n5. Employees having MI or LA in name:");
console.table(result); } );
con.query(
"SELECT ENAME, HIREDATE FROM EMP WHERE DEPTNO = 10",
(err, result) => {
console.log("\n6. Employees from Dept 10 (Name & Hire Date):");
console.table(result); } );
con.query(
"UPDATE EMP SET SAL = SAL - 100 WHERE DEPTNO = 10",
(err, result) => {
console.log("\n7. Salary reduced by Rs.100 for Dept 10 employees");
console.log("Rows affected:", result.affectedRows); } ); });
- Write a Node.Js code to get the relevant data from the above table/s - Delete all records whose salary is greater than Rs.3500/- - Update the commission of all employees who are in department 30 to
Rs.650/-
const mysql = require('mysql');
const con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "HR" });
con.connect((err) => {
if (err) {
console.log("Connection failed");
return; }
console.log("Connected to HR Database\n");
con.query(
"DELETE FROM EMP WHERE SAL > 3500",
(err, result) => {
if (err) throw err;
console.log("Employees with salary > 3500 deleted");
console.log("Rows deleted:", result.affectedRows);}
);
con.query(
"UPDATE EMP SET COMM = 650 WHERE DEPTNO = 30",
(err, result) => {
if (err) throw err;
console.log("\nCommission updated to Rs.650 for Dept 30 employees");
console.log("Rows updated:", result.affectedRows);
} ); });
ASSIGNMENT - 4
- Creating the First React Application and setting up the react environment.
npx create-react-app myapp
cd appname
npm start
2. Write a react code for implementing react component – function
import React from 'react';
function MyComponent() {
return (
<?div>
<?h1>Welcome to React
<?p>This is a Function Component.
); }
export default MyComponent;
3. Write a React code for implementing React Component – Class
import React, { Component } from 'react';
class MyComponent extends Component {
render() {
return (
<?div>
<?h1>Welcome to React
<?p>This is a Class Component.
<?/div> );
}
}
export default MyComponent;
- Write a react application to render a Table.
import React from 'react';
function App() { return
( <?div>
<?h2>Employee Details
<?table border="1" cellPadding="10">
<?thead>
<?tr>
<?th>Emp ID
<?th>Name
<?th>Department
<?th>Salary
|
<?tbody>
<?tr>
<?td>101
<?td>Aryan
<?td>HR
<?td>30000
// make more table