-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path27-METHODS-Put-Delete.js
More file actions
63 lines (52 loc) · 1.82 KB
/
Copy path27-METHODS-Put-Delete.js
File metadata and controls
63 lines (52 loc) · 1.82 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
const express = require('express');
const { people } = require('./node-express-course/02-express-tutorial/data');
const app = express()
//static asset
app.use(express.static('node-express-course/02-express-tutorial/methods-public'))
//parse from data
app.use(express.urlencoded({extended: false})) //in order to get the data on our terminal
app.use(express.json())
app.post('/login',(req,res)=>{
const {name} = req.body;
if (name){
console.log(req.body);
return res.status(200).send(`Welcome ${name}`)
}
res.status(401).send('name toh daal le ')
})
app.get('/api/people',(req,res)=>{
res.status(200).json({sucess:true,data:people})
})
app.post('/api/people',(req,res)=>{
const {name} = req.body;
if (!name) {
return res.status(410).json({success :false,msg:'plz provide data'})
}
res.status(201).json({success:true,person:name})
})
app.put('/api/people/:id', (req,res)=>{
const { id } = req.params
const { name } = req.body
const person = people.find((person)=> person.id === Number(id))
if (!person) {
return res.status(404).json({success :false,msg:`no person with id ${id}`})
}
const newPerson = people.map((person)=>{
if(person.id === Number(id)){
person.name = name
}
return person
})
res.status(200).json({success:true,data:newPerson})
})
app.delete('/api/people/:id',(req,res)=>{
const person = people.find((person)=> person.id === Number(req.params.id))
if (!person) {
return res.status(404).json({success :false,msg:`no person with id ${req.params.id}`})
}
const newPeople = people.filter((person) => person.id !== Number(req.params.id))
return res.status(200).json({success:true,data:newPeople})
})
app.listen(5009,()=>{
console.log('server is listening to port : 5009');
})