Mern lab Manual
Mern lab Manual
Using MongoDB, create a collection called transactions in database usermanaged (drop if it already exists)
and bulk load the data from a json file, transactions.json
Upsert the record from the new file called transactions_upsert.json in Mongodb.
Transaction.js
[
{
"_id": 1,
"name": "Transaction 1",
"amount": 100
},
{
"_id": 2,
"name": "Transaction 2",
"amount": 200
},
{
"_id": 3,
"name": "Transaction 3",
"amount": 300
}
]
Transaction_upsert.js
[
{
"_id": 2,
"name": "Updated Transaction 2",
"amount": 250
},
{
"_id": 4,
"name": "New Transaction 4",
"amount": 400
}
]
Program 2:
Query MongoDB with Conditions: [Create appropriate collection with necessary documents to answer the
query]
[
{
"_id": 1,
"Name": "Somu",
"Payment": {
"Total": 600
},
"Transaction": {
"Price": 400
}
},
{
"_id": 2,
"Name": "Ravi",
"Payment": {
"Total": 500
},
"Transaction": {
"Price": 350
}
},
{
"_id": 3,
"Name": "Somu",
"Payment": {
"Total": 700
},
"Transaction": {
"Price": 450
}
},
{
"_id": 4,
"Name": "Kumar",
"Payment": {
"Total": 800
},
"Transaction": {
"Price": 550
}
}
]
{ "Name": "Somu" }
{ "Payment.Total": 600 }
c.Find any record where price(Transaction.price) is between 300 to 500.
[
{
$group: {
_id: null,
totalAmount: { $sum: "$Payment.Total" }
}
}
]
Program 3a.
Write a program to check request header for cookies.
// Create a server
const server = http.createServer((req, res) => {
// Get request headers
const headers = req.headers;
console.log(headers, ".....");
// Check if 'cookie' header exists
if (headers.cookie) {
console.log("Cookies:", headers.cookie);
} else {
console.log("No cookies found in the request header.");
}
Output:
Program 3b.
write node.js program to print the a car object properties, delete the second property and get length of the
object.
// API endpoint to delete the second property and get object length
app.delete("/api/car/:propertyIndex", (req, res) => {
const propertyIndex = parseInt(req.params.propertyIndex);
if (
isNaN(propertyIndex) ||
propertyIndex < 0 ||
propertyIndex >= Object.keys(car).length
){
return res.status(400).json({ error: "Invalid property index" });
}
res.json({
deletedProperty: propertyToDelete,
remainingProperties: Object.keys(car),
objectLength: Object.keys(car).length,
});
});
Output:
Program 5.
Implement all CRUD operations on a File System using Node JS
/*
!run one function at a time
*/
const fs = require("fs");
// readFile("example.txt");
// deleteFile("example.txt");
Output :
Program 6.
Develop the application that sends fruit name and price data from client side to Node.js server using Ajax
import React, { useState } from "react";
import axios from "axios";
function App() {
const [name, setName] = useState("");
const [price, setPrice] = useState("");
const url = "http://localhost:5000";
const handleSubmit = async (e) => {
e.preventDefault();
try {
await axios.post(`${url}/api/fruits`, { name, price });
console.log("Data sent successfully");
// Optionally, you can reset the form fields here
} catch (error) {
console.error("Error sending data:", error);
}
};
return (
<div>
<h1>Fruit Data Form</h1>
<form onSubmit={handleSubmit}>
<label>
Fruit Name:
<input
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
/>
</label>
<br />
<label>
Price:
<input
type="text"
value={price}
onChange={(e) => setPrice(e.target.value)}
/>
</label>
<br />
<button type="submit">Submit</button>
</form>
</div>
);
}
Output:
Program 7:
Develop an authentication mechanism with email_id and password using HTML and Express JS (POST
method)
function App() {
const [registerData, setRegisterData] = useState({ email: "", password: "" });
const [loginData, setLoginData] = useState({ email: "", password: "" });
const [message, setMessage] = useState("");
const handleRegister = async (e) => {
e.preventDefault();
try {
await axios.post("http://localhost:3000/register", registerData);
setMessage("User registered successfully");
} catch (error) {
setMessage("Error registering user");
console.error("Error registering user:", error);
}
};
return (
<div>
<h2>User Registration</h2>
<form onSubmit={handleRegister}>
<label>Email:</label>
<input
type="email"
value={registerData.email}
onChange={(e) =>
setRegisterData({ ...registerData, email: e.target.value })
}
required
/>
<br />
<label>Password:</label>
<input
type="password"
value={registerData.password}
onChange={(e) =>
setRegisterData({ ...registerData, password: e.target.value })
}
required
/>
<br />
<button type="submit">Register</button>
</form>
<h2>User Login</h2>
<form onSubmit={handleLogin}>
<label>Email:</label>
<input
type="email"
value={loginData.email}
onChange={(e) =>
setLoginData({ ...loginData, email: e.target.value })
}
required
/>
<br />
<label>Password:</label>
<input
type="password"
value={loginData.password}
onChange={(e) =>
setLoginData({ ...loginData, password: e.target.value })
}
required
/>
<br />
<button type="submit">Login</button>
</form>
Output:
Program 8:
Develop two routes: find_prime_100 and find_cube_100 which prints prime numbers less than 100 and
cubes less than 100 using Express JS routing mechanism
function App() {
return (
<div>
<SearchFilter />
</div>
);
}
Output :
Program 10:
Develop a React code to collect data from rest API.
import React, { useState, useEffect } from "react";
import axios from "axios";
function DataCollector() {
const [data, setData] = useState([]);
useEffect(() => {
fetchData();
}, []);
return (
<div>
<h2>Data Collection</h2>
<button onClick={fetchData}>Fetch Data</button>
<ul>
{data.map((item, index) => (
<li key={index}>{item.employee_name}</li>
))}
</ul>
</div>
);
}