Practical No - 10 - AWT
Practical No - 10 - AWT
Practical No 10
Q1 Create a node.js program using express.js that serves a list of users from a JSON file. The
program should define an API an endpoint to retrieve the details of users in JSON format.
a. Display details of all users b. Display details based on its parameters such as id
Q2 Update the data of the user with user id 2 using patch method
Q3. Delete a user whose id is 5. Send a response after performing delete operation.
🡪
Users.json
[
{
"id": 1,
"name": "Alice",
"age": 30
}, …..]
Index.js
if (user.id === 2) {
user.name = 'Updated Name';
}
return user;
});
fs.writeFileSync('users.json', JSON.stringify(updatedUsers, null, 2));
res.json({ message: 'User with id 2 updated successfully' });
});
Q4 Connect node.js server to Mongodb compass. Create a database, create a collection and
insert some sure input data
try {
await client.connect();
console.log('Connected to the MongoDB server');
const db = client.db(dbName);
console.log(`Database "${dbName}" created`);
const data = [
{ name: 'John', age: 30 },
{ name: 'Jane', age: 25 },
{ name: 'Doe', age: 40 }
];
const result = await collection.insertMany(data);
console.log(`${result.insertedCount} documents inserted`);
} finally {
await client.close();
console.log('Connection closed');
}
}
main().catch(console.error);