-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (56 loc) · 1.86 KB
/
Copy pathindex.js
File metadata and controls
64 lines (56 loc) · 1.86 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
64
const express = require('express');
const axios = require('axios');
const app = express();
app.use(express.json());
const PORT = 3000;
// Endpoints CRUD de filmes
app.get('/filmes/:id', async (req, res) => {
const { id } = req.params;
try {
const response = await axios.get(`http://localhost:8080/filme/${id}`);
res.json(response.data);
} catch (error) {
res.status(500).send('Erro ao buscar filme');
}
});
app.post('/filmes', async (req, res) => {
try {
console.log("Enviando requisição ao backend Go com dados:", req.body);
const response = await axios.post('http://localhost:8080/filme', req.body);
console.log("Resposta do backend Go:", response.data);
res.status(201).json(response.data);
} catch (error) {
console.error("Erro ao adicionar filme:", error);
res.status(500).send('Erro ao adicionar filme');
}
});
app.get('/filmes', async (req, res) => {
try {
const response = await axios.get('http://localhost:8080/filmes');
res.json(response.data);
} catch (error) {
console.error("Erro ao obter filmes:", error);
res.status(500).send('Erro ao obter filmes');
}
});
app.put('/filmes/:id', async (req, res) => {
const { id } = req.params;
try {
const response = await axios.put(`http://localhost:8080/filme/${id}`, req.body);
res.json(response.data);
} catch (error) {
res.status(500).send('Erro ao atualizar filme');
}
});
app.delete('/filmes/:id', async (req, res) => {
const { id } = req.params;
try {
const response = await axios.delete(`http://localhost:8080/filme/${id}`);
res.status(204).send();
} catch (error) {
res.status(500).send('Erro ao deletar filme');
}
});
app.listen(PORT, () => {
console.log(`Servidor rodando em http://localhost:${PORT}`);
});