-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
78 lines (63 loc) · 1.99 KB
/
Copy pathapp.js
File metadata and controls
78 lines (63 loc) · 1.99 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
var express = require("express");
var app = express();
var parser = require("body-parser");
var posts = require('./db/post_db');
app.use(parser.json());
app.use(parser.urlencoded({extended:false}));
app.get('/api/v1/allposts',(req,res,next)=>{
res.status(200).send({
success:'true',
message:'all posts retrieved successfully',
allposts:posts
})
});
app.post('/api/v1/create/post',(req,res,next)=>{
if(!req.body.topic){
res.status(400).send({
success:'false',
message:"topic must be provided",
})
}else if(!req.body.description){
res.status(400).send({
success:'false',
message:'description must be provided'
})
}else if(!req.body.author){
res.status(400).send({
success:'false',
message:'author must be provided'
})
}
var NewPost=
{
id: posts.length +1,
topic: req.body.topic,
description: req.body.description,
author : req.body.author
}
posts.push(NewPost);
return res.status(201).send({
success:'true',
message:'A new post has been created'
})
})
app.delete('/api/v1/posts/:id',(req,res)=>{
const id = parseInt(req.params.id,10);
posts.map((allposts,index)=>{
if(allposts.id===id){
posts.splice(index,1);
return res.status(200).send({
success:'true',
message:"Post deleted successfully"
});
}
})
return res.status(404).send({
success:'false',
message:'No posts found to delete'
});
})
app.listen(3000, ()=>{
console.log("Server running on port 3000");
})
module.exports=app