-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostApi.js
44 lines (36 loc) · 986 Bytes
/
postApi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const express = require('express');
const dbConnect = require('./dbConnFile');
const mongodb=require('mongodb');
const app = express();
//telling the node that we are using json data formate
app.use(express.json());
//get api
app.get('/' , async (req , res) => {
let data =await dbConnect();
data = await data.find().toArray();
console.log(data);
res.send(data);
});
//post api
app.post('/' , async (req , res) => {
let data = await dbConnect();
let result = await data.insert(req.body);
res.send(result);
});
//put api
app.put('/:xyz' , async(req ,res)=>{
let data = await dbConnect();
let result = data.updateOne(
{name:req.params.xyz} ,
{$set:req.body}
)
res.send({result:"update"})
}) ;
//delete API
app.delete("/:id" , async(req , res)=>{
console.log(req.params.id);
const data = await dbConnect();
const result = await data.deleteOne({_id:new mongodb.ObjectId(req.params.id)});
res.send(result);
})
app.listen(8000);