-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
51 lines (44 loc) · 1.13 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
import express from "express";
import {users} from './data.js'
const app =express()
app.use(express.json())
app.get('/users',(res, req) => {
res.json(users)
})
app.get('/users/:id', (req, res) =>{
const {id} = req.params
const user = users.filter((user) => user.id === id)
res.json(user)
})
app.post('/users', (req, res)=> {
try{
const {id, name} = req.body;
console.log(id, name);
users.push({id, name});
res.json(users)
} catch (error) {
res.json(error)
}
})
app.delete('/users/:id', (req, res) => {
const {id} = req.params
res.json(users.filter((user) => user.id !==id ))
})
app.patch('/users/:id', (req, res)=> {
const {id} = req.params
const {name} = req.body
let user1 =users.find((user) => user.id === id)
user1.name = name
res.json(user1)
})
app.put('/users/:id', (req, res)=> {
const {id} = req.params
const {name} = req.body
let user1 =users.find((user) => user.id === id)
user1.name = name
res.json(user1)
})
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is up and running on port ${PORT}`);
});