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
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);
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); } });
It's like having a super-smart buddy that is always there to help you write stories,…
The UK is known for its rich history, diverse culture, and most of all its…
Do you have a plan for your next holiday? Being aware of the Bank Holidays within the…
The world is rapidly changing of software development AI-assisted tools for coding have become the main focus. As…
Google Gemini AI is among the top talked about developments. What exactly is it? What…
As the need for AI content undetectable humanization software skyrockets with more and more AI…