-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress-backup
More file actions
47 lines (46 loc) · 1.05 KB
/
Copy pathexpress-backup
File metadata and controls
47 lines (46 loc) · 1.05 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
const express=require('express')
const app=express()
//configuration- enable express to parse incoming json data
app.use(express.json())
const port=6002
const customers=[
{id:1,name:'lasya'},
{id:2,name:'krishna'}
]
//request handlers
//syntax: app.httpmethod(url,callback)
app.get('/',(req,res)=>{
res.send('welcome to website')
})
app.get('/customers',(req,res)=>{
res.json(customers)
})
//finding customer based on id
app.get('/customers/:id',(req,res)=>{
const id=req.params.id
const customer=customers.find(customer =>customer.id==id)
if(customer){
res.json(customer)
}
else{
res.json({})
}
})
//post create
app.post('/customers',(req,res)=>{
const body=req.body
res.json(body)
})
//put
app.put('/customers/:id',(req,res)=>{
const id=req.params.id
res.send(` id updated`)
})
//delete
app.delete('/customers/:id',(req,res)=>{
const id=req.params.id
res.send(` deleted `)
})
app.listen(port,()=>{
console.log('server running on port',port)
})