Skip to content

Commit

Permalink
post , get , put and delete API using mongoose
Browse files Browse the repository at this point in the history
  • Loading branch information
codingXpert committed Jul 31, 2022
1 parent 8d702e6 commit 7da9d79
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
25 changes: 25 additions & 0 deletions Index3.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,37 @@ const product = require('./product');
const app = express();
app.use(express.json()) //converts the data into JSON formate forom stream formate

//post API using mongoose
app.post("/create" , async (req,res)=>{
let data = new product(req.body);
let result = await data.save();
console.log(result);
res.send(result);
});

//get API using mongoose
app.get("/list" , async(req,res)=>{
let data = await product.find();
res.send(data);
});

// delete Api using mongoose
app.delete("/delete/:_id" , async(req , res)=>{
console.log(req.params)
let data = await product.deleteOne(req.params);
res.send(data);
});

// put API using mongoose
app.put("/update/:_id" , async(req , res)=>{
console.log(req.params);
let data = await product.updateOne(
req.params ,
{
$set:req.body
}
);
res.send(data);
});
const port= 8000;
app.listen(port);
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/e-comm");
mongoose.connect("mongodb://localhost:27017/e-comm");
2 changes: 1 addition & 1 deletion product.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ const productSchema = new mongoose.Schema({
});

//Model
module.exports = mongoose.model('products' , productSchema);
module.exports = mongoose.model('products' , productSchema);

0 comments on commit 7da9d79

Please sign in to comment.