-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
61 lines (46 loc) · 1.34 KB
/
server.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
require('dotenv').config()
const app = require('./app')
const port = 3000
require('./database')
require('./redis/blocklist-access-token')
require('./redis/allowlist-refresh-token')
const { InvalidArgumentError, NotFound, NotAuthorized } = require('./src/erros')
const { ConversorErro } = require('./src/conversores')
const jwt = require('jsonwebtoken')
app.use((req, res, next) => {
const accept = req.get('Accept');
if (accept.indexOf('application/json') === -1 && accept !== '*/*') {
res.status(406);
res.end();
return;
}
res.set({'Content-Type': 'application/json'});
next();
})
const routes = require('./rotas')
routes(app)
app.use((error, req, res, next) => {
let status = 500;
const corpo = {
mensagem: error.message
};
if (error instanceof InvalidArgumentError) {
status = 400;
}
if (error instanceof jwt.JsonWebTokenError) {
status = 401;
}
if (error instanceof jwt.TokenExpiredError) {
status = 401;
corpo.expiredAt = error.expiredAt;
}
if (error instanceof NotFound) {
status = 404;
}
if (error instanceof NotAuthorized) {
status = 401;
}
const conversor = new ConversorErro('json');
res.send(conversor.converter(corpo));
});
app.listen(port, () => console.log('A API está funcionando!'))