-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (36 loc) · 855 Bytes
/
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
const { urlencoded } = require('express');
const express = require('express');
const app = express();
let accessHistory = [
{
url: 'https://victor.dev.br',
time: '06 Jun 2021',
},
];
app.use(express.json());
app.get('/api/history', (req, res) => {
res.send(JSON.stringify(accessHistory));
});
app.post('/api/history', (req, res) => {
const { url, time } = req.body;
try {
if (!url || url.length == 0) {
throw new Error('URL not found or invalid');
}
if (!time || time.length == 0) {
throw new Error('Time not found or invalid');
}
accessHistory.push({
url: url,
time: time,
});
res.status(201);
res.end();
} catch (error) {
res.status(400);
res.send({
message: error.message,
});
}
});
app.listen(3000, () => console.log('A API está funcionando'));