-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
43 lines (29 loc) · 879 Bytes
/
server.js
File metadata and controls
43 lines (29 loc) · 879 Bytes
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
const express = require("express");
const app = express();
const data = require('./data.json');
app.use(express.json());
app.get("/client", function(req, res){
res.json(data);
});
app.get("/client/:id", function(req, res){
const {id} = req.params;
const client = data.find(cli => cli.id == id);
if (!client) return res.status(404).json();
else res.json(client);
});
app.post("/client", function(req, res){
const {id, name, email} = req.body;
res.json({id, name, email})
});
app.put("/client/:id", function(req, res){
const {id} = req.params;
const client = data.find(cli => client.id = id)
const {name, email} = req.body;
if(!client) return res.status(204);
else
client.name = name, client.email = email;
res.json(client);
});
app.listen(3000, function() {
console.log('Server is running!');
});