-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (46 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
const express = require('express');
const axios = require('axios');
const { Dog } = require('./database/models');
const app = express();
const port = 3000;
const indexPage = `
<h3>Hello from a Node.js Application running on AWS EC2</h3>
<p>What would you like to see?</p>
<ul>
<li>Random Dogs? (External API Call) <a href='/dogs'>Click here</a></li>
<li>List All Dogs on RDS Intance? <a href='/data'>Click here</a></li>
</ul>
`
app.use(express.json());
app.get('/', (_req, res) => res.send(indexPage));
app.get('/healthcheck', (_req, res) => {
try {
res.sendStatus(200);
} catch (error) {
res.sendStatus(500);
}
});
app.get('/dogs', async (_req, res) => {
try {
const response = await axios.get('https://dog.ceo/api/breeds/image/random');
console.log(JSON.stringify(response.data));
const { message: dogImage } = response.data;
res.send(
`<img src='${dogImage}' alt='random-dog' style='max-width: 500px' />`
);
} catch (error) {
console.error(error);
res.status(500);
res.send(error.message);
}
});
app.get('/data', async(_req, res) => {
try {
const dogs = await Dog.findAll();
return res.status(200).json(dogs);
} catch (error) {
console.log(error.message);
return res.status(500).json({ message: 'Ocorreu um erro' });
}
});
app.listen(port, () => console.log(`App listening on port ${port}!`));