-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
49 lines (42 loc) · 1.28 KB
/
api.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
import express from 'express';
import getImoveis from './controllers/imoveis.js';
import getImovelMaisCaro from './controllers/mais-caro.js';
import getImovelMaisBarato from './controllers/mais-barato.js';
const app = express();
// Middleware para tratamento de erros
const errorHandler = (error, req, res, next) => {
console.error('Erro durante a execução do Puppeteer:', error);
res.status(500).json({ error: 'Erro durante a execução do Puppeteer.' });
};
// Rotas
app.get('/api/imoveis/:local', async (req, res, next) => {
try {
const data = await getImoveis(req.params.local);
res.json(data);
} catch (error) {
next(error);
}
});
app.get('/api/imoveis/:local/mais-barato', async (req, res, next) => {
try {
const data = await getImovelMaisBarato(req.params.local);
res.json(data);
} catch (error) {
next(error);
}
});
app.get('/api/imoveis/:local/mais-caro', async (req, res, next) => {
try {
const data = await getImovelMaisCaro(req.params.local);
res.json(data);
} catch (error) {
next(error);
}
});
// Inicialização do servidor
const port = 3000; // Escolha uma porta disponível
app.listen(port, () => {
console.log(`Servidor rodando na porta ${port}`);
});
// Tratamento de erros
app.use(errorHandler);