-
-
Notifications
You must be signed in to change notification settings - Fork 750
Expand file tree
/
Copy pathserver.js
More file actions
68 lines (59 loc) · 1.78 KB
/
server.js
File metadata and controls
68 lines (59 loc) · 1.78 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
const express = require('express')
const app = express()
app.use(express.json())
// Example users data
let users = [
{ id: 1, name: 'John Doe', email: 'john@example.com' },
{ id: 2, name: 'Jane Smith', email: 'jane@example.com' },
]
// Example comments data
let comments = [{ id: 1, postId: 1, text: 'Great post!' }]
// GET /api/users
app.get('/api/users', (req, res) => {
res.json({ data: users })
})
// GET /api/comments/:id
app.get('/api/comments/:id', (req, res) => {
const comment = comments.find(c => c.id === parseInt(req.params.id))
if (comment) {
return res.json({
data: comment,
support: {
url: 'http://example.com/support',
text: 'Support information',
},
})
}
res.status(404).json({ error: 'Comment not found' })
})
// GET /api/users/:id
app.get('/api/users/:id', (req, res) => {
const user = users.find(u => u.id === parseInt(req.params.id))
if (user) return res.json(user)
res.status(404).json({ error: 'User not found' })
})
// POST /api/users
app.post('/api/users', (req, res) => {
const { name, email } = req.body
const newUser = { id: users.length + 1, name, email }
users.push(newUser)
res.status(201).json(newUser)
})
// PUT /api/users/:id
app.put('/api/users/:id', (req, res) => {
const user = users.find(u => u.id === parseInt(req.params.id))
if (!user) return res.status(404).json({ error: 'User not found' })
user.name = req.body.name || user.name
user.email = req.body.email || user.email
res.json(user)
})
// DELETE /api/users/:id
app.delete('/api/users/:id', (req, res) => {
users = users.filter(u => u.id !== parseInt(req.params.id))
res.status(204).send()
})
// Start server
const PORT = process.env.PORT || 3001
app.listen(PORT, () => {
console.log(`Mock REST server running on port ${PORT}`)
})