-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
223 lines (200 loc) · 4.85 KB
/
server.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const express = require('express')
const cors = require('cors')
const logger = require('morgan')
const PORT = process.env.PORT || 3001
const db = require('./db')
const { Post, User, Comment } = require('./models')
const app = express()
app.use(cors())
app.use(express.json())
app.use(logger('dev'))
app.use(express.static(`${__dirname}/client/build`))
///
/// USER API
///
// get all users
app.get('/users', async (req, res) => {
const users = await User.find({})
res.json(users)
})
// get user by id
app.get('/users/:id', async (req, res) => {
try {
const user = await User.findById(req.params.id)
if (!user) throw Error('User not found')
res.json(user)
} catch (e) {
console.log(e)
res.send('User not found!')
}
})
// get user by username
app.get('/user/:username', async (req, res) => {
try {
const user = await User.find({ username: req.params.username })
if (!user) throw Error('User not found')
res.json(user)
} catch (e) {
console.log(e)
res.send('User not found!')
}
})
// create a post
app.post('/user', async (req, res) => {
try {
const user = await new User(req.body)
await user.save()
return res.status(201).json({
user
})
} catch (error) {
return res.status(500).json({ error: error.message })
}
})
///
/// POSTS API
///
// get all posts
app.get('/posts', async (req, res) => {
const posts = await Post.find({})
res.json(posts)
})
// get post by id
app.get('/post/:id', async (req, res) => {
try {
const post = await Post.findById(req.params.id)
if (!post) throw Error('Post not found')
res.json(post)
} catch (e) {
console.log(e)
res.send('Post not found!')
}
})
/// get posts by user id
app.get('/user/posts/:id', async (req, res) => {
try {
const post = await Post.find({ user: req.params.id })
if (!Post) throw Error('Post not found')
res.json(post)
} catch (e) {
console.log(e)
res.send('Post not found!')
}
})
// create a post
app.post('/post', async (req, res) => {
try {
const post = await new Post(req.body)
await post.save()
return res.status(201).json({
post
})
} catch (error) {
return res.status(500).json({ error: error.message })
}
})
// update post
app.put('/post/:id', async (req, res) => {
try {
const post = await Post.findByIdAndUpdate(req.params.id, req.body, {
new: true
})
res.status(200).json(post)
} catch (error) {
return res.status(500).send(error.message)
}
})
// delete
app.delete('/post/:id', async (req, res) => {
try {
const deleted = await Post.findByIdAndDelete(req.params.id)
if (deleted) {
return res.status(200).send('Post deleted')
}
throw new Error('Post not found')
} catch (error) {
return res.status(500).send(error.message)
}
})
///
/// COMMENTS API
///
// get all comments
app.get('/comments', async (req, res) => {
const comments = await Comment.find({})
res.json(comments)
})
// get comment by comment id
app.get('/comment/:id', async (req, res) => {
try {
const comment = await Comment.findById(req.params.id)
if (!comment) throw Error('Comment not found')
res.json(comment)
} catch (e) {
console.log(e)
res.send('Comment not found!')
}
})
/// get comments by user id
app.get('/user/comments/:id', async (req, res) => {
try {
const comment = await Comment.find({ user: req.params.id })
if (!Comment) throw Error('Comment not found')
res.json(comment)
} catch (e) {
console.log(e)
res.send('Comment not found!')
}
})
/// get comments by post id
app.get('/post/comments/:id', async (req, res) => {
try {
const comment = await Comment.find({ post: req.params.id })
if (!Comment) throw Error('Comment not found')
res.json(comment)
} catch (e) {
console.log(e)
res.send('Comment not found!')
}
})
// create a comment
app.post('/comments/:id', async (req, res) => {
try {
const comment = await new Comment(req.body)
await comment.save()
return res.status(201).json({
comment
})
} catch (error) {
return res.status(500).json({ error: error.message })
}
})
// update comment
app.put('/comment/:id', async (req, res) => {
try {
const comment = await Comment.findByIdAndUpdate(req.params.id, req.body, {
new: true
})
res.status(200).json(comment)
} catch (error) {
return res.status(500).send(error.message)
}
})
// delete
app.delete('/comment/:id', async (req, res) => {
try {
const deleted = await Comment.findByIdAndDelete(req.params.id)
if (deleted) {
return res.status(200).send('Comment deleted')
}
throw new Error('Comment not found')
} catch (error) {
return res.status(500).send(error.message)
}
})
app.get('/*', (req, res) => {
res.sendFile(`${__dirname}/client/build/index.html`)
})
app.listen(PORT, () => {
console.log(`Express server listening on port ${PORT}`)
})