0% found this document useful (0 votes)
19 views

CRUD Code Using Mongoose MEAN Stack

This document defines an ExpressJS backend and AngularJS frontend for a basic CRUD application. The Express app connects to a MongoDB database, defines schemas and models, and includes routes to get, post, update, and delete user data. The Angular controller defines functions to get items from the API, add new items, delete items, and update items by making HTTP requests to the Express routes. On success, it updates the items array and on error displays messages. The app provides a basic interface to perform CRUD operations on a MongoDB database using Express and Angular.

Uploaded by

krinamoradiya35
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

CRUD Code Using Mongoose MEAN Stack

This document defines an ExpressJS backend and AngularJS frontend for a basic CRUD application. The Express app connects to a MongoDB database, defines schemas and models, and includes routes to get, post, update, and delete user data. The Angular controller defines functions to get items from the API, add new items, delete items, and update items by making HTTP requests to the Express routes. On success, it updates the items array and on error displays messages. The app provides a basic interface to perform CRUD operations on a MongoDB database using Express and Angular.

Uploaded by

krinamoradiya35
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

App.

js (ExpressJS file)

Third Party module


const express = require('express')
var mongoose = require('mongoose')
const app = express()
//expressJS middleware
app.use(express.json());
app.use(express.static(__dirname))

mongoose.connect("mongodb://0.0.0.0:27017/admin")
var conn = mongoose.connection
conn.on('connected',function(){
console.log("Connected to mongoDB")
})

var userSchema = new mongoose.Schema({


userID:String,
userName:String,
age:{type:Number,required:Boolean,min:20},
userRole:String
})

var user = mongoose.model('user',userSchema,'userData')

app.get('/', (req, res) => {


res.sendFile(__dirname+'/test.html');
});
// Route to get all items
app.get('/api/items', (req, res) => {
user.find().then((data)=>{
res.json(data)
})
});
//Rout to insert new item
app.post('/api/items',(req,res)=>{
user.create({
userID:req.body.userID,
userName:req.body.userName,
age:req.body.age
}).then((newItem)=>{
console.log(newItem)
res.json(newItem)
},(err)=>{
res.json(err);
console.log('Error')
})
// const newData = new users({
// userID:req.body.userID,
// userName:req.body.userName})
// newData.save()
// res.json(newData)
})
//delete
app.delete('/api/delete/:id',(req,res)=>{
let id = req.params.id
user.deleteOne({userID:id}).then((err,data)=>{
res.json(data)
})
// users.findOneAndRemove({userID:id}).then((err,data)=>{
// res.json(data)
// })
})
//update
app.put('/api/update/:id',(req,res)=>{
let id = req.params.id
let updatedItemData = req.body
// user.findOneAndUpdate({userID:id},updatedItemData).then((data)=>{
// res.json(data)
// })
user.updateOne({userID:id},updatedItemData).then((data)=>{
res.json(data)
})

})

app.listen(1000,function(){
console.log("Express server is running on port 1000")
})

Controller.js (Module file)

angular.module('myApp', [])
.controller('myController', function ($scope, $http) {
$scope.items = [];
$scope.stat=0
$scope.errorMsg={}
// Function to get all items from the server
$scope.getItems = function () {
$http.get('/api/items').then(function (response) {
$scope.items = response.data;
});
};

// Function to add a new item to the server


$scope.addItem = function() {
$http.post('/api/items', $scope.newItem).then(function (response) {
$scope.errorMsg = response.data.errors.age.message
if(!$scope.errorMsg){
$scope.items.push(response.data);
$scope.getItems();
$scope.newItem = {};
}

});
};

//function to delete item


$scope.deleteItem = function(item){
$http.delete(`/api/delete/${item.userID}`).then(function(response){
$scope.items = response.data
$scope.getItems();
})
}

//function update item


$scope.updateItem = function(item){
$scope.edit=true
$scope.updatedItem = item
}
$scope.modifyItem = function(up){
$http.put(`/api/update/${up.userID}`,up).then((response)=>{
$scope.items = response.data
})
$scope.getItems();
}
// Initialize the items
$scope.getItems();
});

You might also like