NodeJS - DAY 05
NodeJS - DAY 05
● Database Connection
1. Import Mongoose and Define the MongoDB URL: In the db.js file, you
first import the Mongoose library and define the URL to your MongoDB
database. This URL typically follows the format
mongodb://<hostname>:<port>/<databaseName>. In your code,
you've set the URL to 'mongodb://localhost:27017/mydatabase',
where mydatabase is the name of your MongoDB database.
2. Set Up the MongoDB Connection: Next, you call mongoose.connect()
to establish a connection to the MongoDB database using the URL and
some configuration options (useNewUrlParser, useUnifiedTopology,
etc.). This step initializes the connection process but does not actually
connect at this point.
3. Access the Default Connection Object: Mongoose maintains a default
connection object representing the MongoDB connection. You retrieve this
object using mongoose.connection, and you've stored it in the variable
db. This object is what you'll use to handle events and interact with the
database.
4. Define Event Listeners: You define event listeners for the database
connection using methods like .on('connected', ...),
.on('error', ...), and .on('disconnected', ...). These event
listeners allow you to react to different states of the database connection.
5. Start Listening for Events: The code is set up to listen for events. When
you call mongoose.connect(), Mongoose starts the connection process.
If the connection is successful, the 'connected' event is triggered, and
you log a message indicating that you're connected to MongoDB. If there's
an error during the connection process, the 'error' event is triggered,
and you log an error message. Similarly, the 'disconnected' event can
be useful for handling situations where the connection is lost.
6. Export the Database Connection: Finally, you export the db object, which
represents the MongoDB connection, so that you can import and use it in
other parts of your Node.js application.
To sum it up, the db.js file acts as a central module that manages the
connection to your MongoDB database using Mongoose. It sets up the
connection, handles connection events, and exports the connection object so
that your Express.js server (or other parts of your application) can use it to
interact with the database. When your server runs, it typically requires or
imports this db.js file to establish the database connection before
handling HTTP requests.
{
"name": "Alice",
"age": 28,
"work": "Chef",
"mobile": "123-456-7890",
"email": "[email protected]",
"address": "123 Main St, City",
"salary": 60000
}
https://mongoosejs.com/docs/guide.html
● Parameters:
● Type, required, unique, etc
● What is body-parser
● When a client (e.g., a web browser or a mobile app) sends data to a server,
it typically includes that data in the body of an HTTP request.
● This data can be in various formats, such as JSON, form data, or URL-
encoded data. bodyParser helps parse and extract this data from the
request so that you can work with it in your Express.js application.
● bodyParser processes the request body before it reaches your route
handlers, making the parsed data available in the req.body for further
processing.
● bodyParser.json() automatically parses the JSON data from the
request body and converts it into a JavaScript object, which is then stored
in the req.body
● we need an Endpoint where the client sends data and data needs to be
saved in the database
● Async and await are features in JavaScript that make it easier to work with
asynchronous code, such as network requests, file system operations, or
database queries.
● Using try-and-catch block
● The try block contains the code for creating a new Person document and
saving it to the database using await newPerson.save().
● If an error occurs during any step, it is caught in the catch block, and an
error response is sent with a 500 Internal Server Error status.
● CRUD application
● In any application at the core level, we are always handling the database
● Now we have seen that two methods POST and GET
● GET Methods
● Now Let’s suppose the client wants data on all the persons
● So we need an endpoint for that /person
module.exports = MenuItem;