Mern Exp-11
Mern Exp-11
PRELAB:
In Express.js, middleware are functions that handle HTTP requests and responses.
They sit between the server receiving a request and the server sending a response, allowing you
to perform tasks like authentication, data parsing, and logging. Middleware functions can be
used to process or modify the request or response, making them a powerful tool for building
web applications.
3. What is the purpose of the req and res objects in route handling?
In route handling with web frameworks like Express.js, the `req` (request) object
represents the incoming HTTP request from the client, containing information about the
request, such as parameters, headers, and data sent by the client. The `res` (response) object is
used to craft and send the HTTP response back to the client, including setting headers, status
codes, and sending data. These objects are essential for handling and responding to client
requests in web applications.
- `app.get()` is used to define routes that handle HTTP GET requests. It's typically used for
retrieving data from the server without causing any changes to the server or data.
- `app.post()` is used to define routes that handle HTTP POST requests. It's often used for
submitting data to the server, which can then be processed, stored, or modified.
The key difference is in the type of HTTP request they handle: `app.get()` is for reading data,
while `app.post()` is for sending and processing data.
An Object Document Mapper (ODM), on the other hand, is a higher-level tool used in object-
oriented programming to interact with MongoDB. It maps objects in your application code to
documents in the MongoDB database, providing a more abstract and simplified way to work
with MongoDB, similar to how an ORM (Object-Relational Mapping) works with relational
databases.
In short, a MongoDB driver is a lower-level interface to communicate with MongoDB, while
an ODM is a higher-level tool that helps developers work with MongoDB in a more object-
oriented way.
IN LAB: -
Exercise 1: Create a simple server with nodeJs and import Express framework to create
endpoints.
a. Define a GET route for the homepage which sends “Hellow World” response.
b. The app should listen on a specific port no-3001
Exercise 2: Use mongoose module to create a schema for Users Collection in MongoDB
Local Database. Create a connection URL and database name for local database. Define a
POST route for creating a new user.
Post-Lab:
Question 1: Define a PUT route for updating a user. Define a DELETE route for deleting a
user.
To define PUT and DELETE routes for updating and deleting a user in an
Express.js application, you can use the following code as an example:
```javascript
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
```
- The `PUT` route allows you to update a user's information by specifying their ID in the URL
and providing the updated data in the request body.
- The `DELETE` route allows you to delete a user by specifying their ID in the URL.
Please note that this is a basic example, and in a real-world application, you would typically
interact with a database to update and delete user records.
Question 2: Define a async function that can handle errors occurred from CRUD
operations using try-catch methods.
To define an asynchronous function that can handle errors that occur during CRUD
(Create, Read, Update, Delete) operations using try-catch, you can create an example
function like this:
```javascript
try {
return result;
} catch (error) {
// You can choose to rethrow the error for further handling or return an error
response
// throw error;
performCRUDOperation()
.then(result => {
if (result.error) {
} else {
})
.catch(err => {
});
```
- If an error occurs, it's caught in the `catch` block, where you can handle the error.
You can choose to rethrow the error for further handling or return an error response.
This structure allows you to handle errors gracefully and provide appropriate
responses in your CRUD operations. Remember to replace the
`performActualCRUDOperation` function with your actual database or data-related
operations.