-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
99 lines (83 loc) · 2.88 KB
/
app.js
File metadata and controls
99 lines (83 loc) · 2.88 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const express = require('express');
require('./db/connection');
const Blogs = require('./model/blogCollection');
const app = express();
app.use(express.json());
app.get('/',(req,res)=>{
res.send('hello world');
})
// app.post('/blog',(req,res)=>{
// // console.log(req.body);
// const obj = new Blogs(req.body);
// Promises are basically javascript objects used asynchronously. In a synchronous execution, you don't need states, per se. Your code either successfully execute or fail to execute. In asynchronous code, we execute, wait for callbacks and decide if its success or failure and continue with the synchronous code execution.
// const result = obj.save() //result variable will store promise nothing else
// .then(()=>{
// res.status(201).send(obj);
// console.log('saved')
// })
// .catch((e)=>{
// res.status(400).send(e);
// console.log('error not saved')
// });
// //this console will show promise pending
// console.log("this is not result",result);
// })
//Create blog
app.post('/blog', async(req,res)=>{
// console.log(req.body);
try{
const obj = new Blogs(req.body);
// await only blocks the code execution within the async function. It only makes sure that the next line is executed when the promise resolves. So, if an asynchronous activity has already started, await will not have an effect on it.
const result = await obj.save();
console.log("i am waiting until you not full-fill the promise",result);
res.status(201).send(result);
}catch(e){
res.status(400).send(e);
}
})
//Get Blogs
app.get('/blog', async(req,res)=>{
// console.log(req.body);
try{
const result = await Blogs.find();
console.log(result);
res.status(200).send(result);
}catch(e){
res.status(400).send(e);
}
})
//Get Blog
app.get('/blog/:id', async(req,res)=>{
// console.log(req.params);
try{
const result = await Blogs.findById({_id: req.params.id});
// console.log(result);
res.status(200).send(result);
}catch(e){
res.status(400).send(e);
}
})
//update blog
app.patch('/blog/:id', async(req,res)=>{
// console.log(req.params);
try{
const result = await Blogs.findByIdAndUpdate({_id: req.params.id},{$set: req.body}, {new: true});
// console.log(result);
res.status(200).send(result);
}catch(e){
res.status(400).send(e);
}
})
//Delete Blog
app.delete('/blog/:id', async(req,res)=>{
// console.log(req.params);
try{
const result = await Blogs.findByIdAndDelete({_id: req.params.id});
// console.log(result);
res.status(200).send(result);
}catch(e){
res.status(400).send(e);
}
})
const port = process.env.PORT || 4000;
app.listen(port, ()=>{console.log('Connection listen on port',4000)});