Home Node JS Nodejs Mongodb Cloud Crud Application

Nodejs Mongodb Cloud Crud Application

4 min read
0
0
291

This tutorial will teach you how to make Crud Application using Nodejs Mongodb Cloud Crud Application.

Create the Node Js Project Type on type command prompt

npm init

and press enter key.after that you have the fill configation of project.after done stuff you
will get the package.json file.

after that you have to install the following dependencies

npm i Express
npm i body-parser
npm i mongoose

After done the stuff you have to create the file index.js

index.js

const express = require("express")
const mongoose = require("mongoose")
var routers = require('./routes/routes');
const bodyParser = require("body-parser")

const app = express()

const port = 5000;

const mongodatabaseURL ="mongodb+srv://kobiram:mongoabc@cluster0.cqfkrbj.mongodb.net/?retryWrites=true&w=majority";

mongoose.connect(mongodatabaseURL,{
    useNewUrlParser: true,
    useUnifiedTopology: true
});

const connection = mongoose.connection


app.listen(port,()=>{
    console.log("Server is running port" +port);
})


connection.once("open",()=>{
    console.log("MongoDb Connected!!!......")
});


app.use(bodyParser.json());
app.use(routers);

 

routes.js

const express = require("express")
const router = express.Router();
var studentModel = require('../src/student/studentModel');


//Add Rrcords
router.post('/student/create', async(req,res)=>{
   
    const student = new studentModel(req.body)
    try{
            await student.save();
            res.status(201).send(
                {
                    "status" : true,
                    "message" : "Student Created!!!!!!!!!!!!!!!!"
                });
    }
    catch(error)
    {
        res.status(400).send(error);

    }


});

//View Records
router.get('/students', async(req,res)=>{
   
   try{

        const students = await studentModel.find({});
        res.send(students);
   }
   catch(error)
   {
        res.status(400).send(error);
   }

});


//find records

router.get('/students/:id', async(req,res)=>{
   
    try{
         const _id = req.params.id;
         const students = await studentModel.findById({_id});

        if(!students)
        {
            return res.status(404).send();
        }  
        return res.status(200).send(students); 
    }
    catch(error)
    {
         res.status(400).send(error);
 
    }

 
 });


//update records
 router.patch('/students/:id', async(req,res)=>{
   
    try{
        const _id = req.params.id;
        const body = req.body;
        const updatestudents = await studentModel.findByIdAndUpdate(_id,body,{new:true});

        if(!updatestudents)
        {
            return res.status(404).send();
        }  
     
        res.status(201).send(
            {
                "status" : true,
                "message" : "Student updateddd!!!!!!!!!!!!!!!!"
            });
 
    }
    catch(error)
    {
         res.status(400).send(error);
 
    }

 
 });


//delete records
 router.delete('/students/:id', async(req,res)=>{
   
    try{
            const _id = req.params.id;
        
         const deletestudents = await studentModel.findByIdAndDelete(_id);

        if(!deletestudents)
        {
            return res.status(404).send();
        }  
       
        res.status(201).send(
            {
                "status" : true,
                "message" : "Student Deletedd!!!!!!!!!!!!!!!!"
            });
    }
    catch(error)
    {
         res.status(400).send(error);
 
    }
 });

 

i have attached the video link below. which will do this tutorials step by step.

 

 

 

Load More Related Articles
Load More By admin
Load More In Node JS

Leave a Reply

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

Check Also

Employee Working Hours Calculation System using C#.net

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the f…