-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (73 loc) · 2.25 KB
/
index.js
File metadata and controls
87 lines (73 loc) · 2.25 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
const express = require('express');
const app = express();
const db = require('./config/db');
const Post = require('./models/Post');
const port = 3000;
//to parse the data to body
app.use(express.json());
db().then(()=> console.log('Database connected successfully')).catch(err => console.log(err));
//to check the health of API
app.get('/api/', (req, res) => {
//res.send('API is working fine')
//to like json format
res.status(200).json({message : 'API is working fine'})
})
//fetching all posts
app.get('/api/posts', (req, res) => {
Post.find({}).then((data) => {
//console.log(data)
res.status(200).json({data : data})
}).catch((error) => {
//console.log(error)
res.status(500).json({message : error})
})
})
//to get a specific post
app.get('/api/posts/:id', (req, res) => {
let postId = req.params.id;
Post.find({_id : postId}).then((data) => {
res.status(200).json({data : data})
}).catch((error) => {
res.status(500).json({message : error})
})
})
//create a new post
app.post('/api/posts', (req, res) => {
let newPost = new Post ({
title : req.body.title,
description : req.body.description
})
//console.log(newPost)
newPost.save().then((data) => {
res.status(200).json({message: "Post created successfully", data: data})
}).catch((error) => {
res.status(500).json({message : error})
})
})
//updating a specific post
app.put('/api/posts/:id', (req, res) => {
let postId = req.params.id;
let newInfo = {
title : "Updated Title of Post",
description : "Updated Description"
}
Post.findByIdAndUpdate(postId, newInfo).then((data) => {
res.status(200).json({message: "Post updated successfully"})
}).catch((error) => {
res.status(500).json({message : error})
})
})
//deleting a specific post
app.delete('/api/posts/:id', (req, res) => {
let postId = req.params.id;
Post.findByIdAndDelete(postId).then((data) => {
res.status(200).json({message: "Post deleted successfully"})
}).catch((error) => {
res.status(500).json({message : error})
})
})
app.listen(port, (err) => {
if(!err){
console.log(`Server is up and running at ${port}`);
}
});