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

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

Leave a Reply

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