MERN Progress
MERN Progress
js,
Node.js) Laboratory Manual
Course Information
Course Code: BDSL456C
Teaching Hours/Week (L:T:P: S): 0:0:2:0
Credits: 01
CIEMarks: 50
SEEMarks: 50
ExamHours: 02
Examination Type (SEE): Practical
Course Objectives
1. Understand and apply critical web development languages and tools to create
dynamic and responsive web applications.
2. Build server-side applications using Node.js and Express.
3. Develop user interfaces with React.js.
4. Manage data using MongoDB and integrate these technologies to create full stack
applications.
5. Understand APIs and routing.
Experiments
Experiment 1: Working with MongoDB Collections
Objective:
Tasks:
Instructions:
Code Sample:
try {
await client.connect();
const db = client.db('usermanaged');
const collection = db.collection('transactions');
run().catch(console.dir);
Objective:
Tasks:
1. Create the necessary collections with documents to perform the following queries:
o Find any record where Name is Somu.
o Find any record where the total payment amount (Payment.Total) is 600.
o Find any record where price (Transaction.price) is between 300 to 500.
o Calculate the total transaction amount by adding up Payment.Total in all
records.
Instructions:
Code Sample:
try {
await client.connect();
const db = client.db('usermanaged');
const collection = db.collection('transactions');
runQueries().catch(console.dir);
Objective:
● To interact with HTTP request headers and manage object properties in Node.js.
Tasks:
Instructions:
1. Use Node.js modules like http to create a server and access request headers.
2. Manage object properties using standard object manipulation techniques.
Code Sample:
delete car.model;
Objective:
Tasks:
1. Read the data of a student containing usn, name, sem, and year of admission from
Node.js and store it in MongoDB.
2. For a partial name given in Node.js, search all the names from MongoDB student
documents created in Task (a).
Instructions:
1. Use MongoDB's insertOne and find methods to store and search data.
Code Sample:
try {
await client.connect();
const db = client.db('school');
const collection = db.collection('students');
manageStudentData().catch(console.dir);
Objective:
Tasks:
1. Create a file, read its content, update the content, and delete the file.
Instructions:
const fs = require('fs');
// Create a file
fs.writeFile('example.txt', 'Hello, world!', (err) => {
if (err) throw err;
console.log('File created.');
Objective:
● To develop an application that sends fruit name and price data from client-side to
Node.js server using Ajax.
Tasks:
Instructions:
1. Use HTML, jQuery (or vanilla ), and Node.js http or express module.
Code Sample:
html
<script>
$('#fruitForm').on('submit', function(e) {
e.preventDefault();
const fruitData = {
name: $('#name').val(),
price: $('#price').val()
};
$.ajax({
type: 'POST',
url: '/submit-fruit',
data: JSON.stringify(fruitData),
contentType: 'application/json',
success: function(response) {
console.log('Data sent to server:', response);
}
});
});
</script>
</body>
</html>
// Node.js Server
const express = require('express');
const app = express();
const port = 3000;
app.use(express.json());
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});
Objective:
Tasks:
1. Create a simple HTML form for login.
2. Use Express.js to handle the POST request and validate the credentials.
Instructions:
1. Use HTML for the form and Express.js for server-side validation.
Code Sample:
html
// Express.js Server
const express = require('express');
const app = express();
const port = 3000;
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});
Objective:
Tasks:
1. Develop two routes:
o find_prime_100 to print prime numbers less than 100.
o find_cube_100 to print cubes of numbers less than 100.
Instructions:
Code Sample:
function findPrimes(limit) {
const primes = [];
for (let i = 2; i < limit; i++) {
let isPrime = true;
for (let j = 2; j <= Math.sqrt(i); j++) {
if (i % j === 0) {
isPrime = false;
break;
}
}
if (isPrime) primes.push(i);
}
return primes;
}
function findCubes(limit) {
const cubes = [];
for (let i = 1; i * i * i < limit; i++) {
cubes.push(i * i * i);
}
return cubes;
}
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});
Objective:
Tasks:
1. Create a React component that displays a list of items.
2. Implement a search filter to display a filtered list based on user input.
Instructions:
Code Sample:
return (
<div>
<input type="text" placeholder="Search..." value={search}
onChange={handleSearch} />
<ul>
{filteredItems.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
</div>
);
};
Objective:
Tasks:
Instructions:
useEffect(() => {
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => {
setData(data);
setLoading(false);
})
.catch(error => console.error('Error fetching data:', error));
}, []);
if (loading) {
return <div>Loading...</div>;
}
return (
<div>
<ul>
{data.map((item, index) => (
<li key={index}>{item.name}</li>
))}
</ul>
</div>
);
};