0% found this document useful (0 votes)
9 views

Backend Exp 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Backend Exp 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

WORKSHEET-2

Student Name: Satvik kushwaha UID: 23MCA20078

Branch: MCA Section/Group: 3/A

Semester: Third Date of Performance: 14-08-2024

Subject Name: Design and Analysis of Algorithms Subject Code: 23CAH-705

1) Aim:
To store the data from our previous experiment (employee salary program) in a file and then use that file in
another program, we can follow these steps:
1. Store Data in a JSON File: Ensure that our main program writes the data to a JSON file
2. Read Data from JSON File in Another Program: Create a new Node.js program that reads the data from the
JSON file and performs some operations on it.

2) Code:

Server.js

const express = require('express');


const fs = require('fs');
const app = express();
const PORT = 3000;

app.get('/data', (req, res) => {


fs.readFile('data.json', 'utf8', (err, data) => {
if (err) {
res.status(500).send('Error reading file');
return;
}
res.setHeader('Content-Type', 'application/json');
res.send(data);
});
});

app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Data.json
[
{
"id": 1,
"name": "satvik",
"age": 22,
"position": "Software Engineer",
"email": "[email protected]"
},
{
"id": 2,
"name": "adarsh",
"age": 22,
"position": "Project Manager",
"email": "[email protected]"
},
{
"id": 3,
"name": "aryan",
"age": 22,
"position": "UX Designer",
"email": "[email protected]"
}
]

Index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Employee Data</title>
</head>
<body>
<h1>Employee Data Management system</h1>
<button id="loadDataButton">Load Employee Data</button>
<div id="employees"></div>

<script>
function loadEmployeeData() {
fetch("http://localhost:3000/data")
.then((response) => response.json())
.then((employees) => {
const employeesDiv = document.getElementById("employees");
employeesDiv.innerHTML = "";
employees.forEach((employee) => {
const employeeDiv = document.createElement("div");
employeeDiv.className = "employee";
employeeDiv.innerHTML = `
<h2>${employee.name}</h2>
<p><strong>ID:</strong> ${employee.id}</p>
<p><strong>Age:</strong> ${employee.age}</p>
<p><strong>Position:</strong> ${employee.position}</p>
<p><strong>Email:</strong> ${employee.email}</p>
`;
employeesDiv.appendChild(employeeDiv);
});
})
.catch((error) => console.error("Error:", error));
}

document
.getElementById("loadDataButton")
.addEventListener("click", loadEmployeeData);
</script>
</body>
</html>

3) Output:

Learning outcomes

a) Various usage of FS module. b) API calls using the fetch method.

You might also like