-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathserver.js
50 lines (34 loc) · 1006 Bytes
/
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
const express = require("express");
const app = express();
const data = require("./data.json");
app.use(express.json());
app.get("/clients", function(req, res) {
res.json(data);
});
app.get("/clients/:id", function(req, res) {
const { id } = req.params;
const client = data.find(cli => cli.id == id);
if (!client) return res.status(204).json();
res.json(client);
});
app.post("/clients", function(req, res) {
const { name, email } = req.body;
// salvar
res.json({ name, email });
});
app.put("/clients/:id", function(req, res) {
const { id } = req.params;
const client = data.find(cli => cli.id == id);
if (!client) return res.status(204).json();
const { name } = req.body;
client.name = name;
res.json(client);
});
app.delete("/clients/:id", function(req, res) {
const { id } = req.params;
const clientsFiltered = data.filter(client => client.id != id);
res.json(clientsFiltered);
});
app.listen(3000, function() {
console.log("Server is running");
});