Node js Connect to MongoDB using Mongoose Tutorial

Node js Connect to MongoDB using Mongoose Tutorial

Connect to mongodb using mongoose in node js express tutorial, you will learn how to connect mongoose with node js express application.

MongoDB is a document database built on a scale-out architecture that has become popular with developers of all kinds who are building scalable applications using agile methodologies. MongoDB was built for people who are building internet and business applications who need to evolve quickly and scale elegantly.

How to Connect Mongoose with Node js Express Application

  • Step 1 – Create Node JS App
  • Step 2 – Install Express-Validator and Body Parser Module
  • Step 3 – Create Server.js File
  • Step 4 – Connect App to MongoDB
  • Step 5 – Start App Server

Step 1 – Create Node JS App

Create Node js express app; So, execute the following command on terminal:

mkdir my-app
cd my-app
npm init -yes

Step 2 – Install Mongoose and Body Parser Module

Install mongoose s and body-parser modules into your node js express application by executing the following command on command prompt:

npm install mongoose express body-parser
  • body-parser – Node.js request body parsing middleware which parses the incoming request body before your handlers, and make it available under req.body property. In other words, it simplifies the incoming request.

Step 3 – Create Server.js File

Create Server.js and import above installed modules in it. So visit your app root directory and create Server.js and validate fields; as following:

const express = require("express")
const mongoose = require("mongoose")
const bodyParser = require("body-parser")
const app = express()
const PORT = 3000
app.listen(PORT, () => {
  console.log(`app is listening to PORT ${PORT}`)
})

Step 4 – Connect App to MongoDB

Connect mongoose to local MongoDB instance with db name as testdb; so add the following code into server.js file:

mongoose.connect("mongodb://localhost:27017/testdb", {
  useNewUrlParser: "true",
})
mongoose.connection.on("error", err => {
  console.log("err", err)
})
mongoose.connection.on("connected", (err, res) => {
  console.log("mongoose is connected")
})

Step 5 – Start App Server

Open your command prompt and execute the following command to run node js express file upload application:

//run the below command

node sever.js

Conclusion

Connect to mongodb using mongoose in node js express tutorial, you have learn how to connect mongodb using mongoose with node js express application.

Recommended Node Js Tutorial

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *