-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
63 lines (50 loc) · 1.37 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
const express = require("express");
const bodyParser = require("body-parser");
const handlebars = require("express-handlebars");
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.engine("handlebars", handlebars({ defaultLayout: "main" }));
app.set("view engine", "handlebars");
let usuarios = [];
app.get("/", (req, res) => {
//res.send('ola mundo')
res.render("formulario");
});
app.get("/user", (req, res) => {
res.send(usuarios);
});
app.post("/user", (req, res) => {
let user = {};
user.nome = req.body.nome;
user.id = usuarios.length;
usuarios.push(user);
res.status(301).send("Usuario cadastrado com sucesso");
});
app.put("/user/:id", (req, res) => {
try {
const id = req.params.id;
usuarios[id].nome = req.body.nome;
res.send("Usuario atualizado com sucesso");
} catch (e) {
console.log("Error");
}
});
app.delete("/user/:id", (req, res) => {
try {
const id = req.params.id;
let i = 0;
while (i < usuarios.length) {
if (usuarios[i].id == id) {
usuarios.splice(id, 1);
break;
}
i++;
}
res.send("Usuario deletado com sucesso");
} catch (e) {
console.log("Error");
}
});
const port = process.env.PORT || 3001;
app.listen(port, () => console.log("servidor no ar"));