-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (43 loc) · 1.27 KB
/
app.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
import 'dotenv/config';
import express from 'express';
import path from 'path';
import cors from 'cors';
import Youch from 'youch';
import * as Sentry from '@sentry/node';
import 'express-async-errors';
import routes from './routes';
import sentryConfig from './config/sentry';
import './database';
class App {
constructor() {
this.server = express();
Sentry.init(sentryConfig);
this.middlewares();
this.routes();
this.exceptionHandler();
}
middlewares() {
this.server.use(Sentry.Handlers.requestHandler());
this.server.use(cors()); // setar url se for
this.server.use(express.json());
this.server.use(
'/files',
express.static(path.resolve(__dirname, '..', 'tmp', 'uploads'))
);
}
routes() {
this.server.use(routes);
// The error handler must be before any other error middleware and after all controllers
this.server.use(Sentry.Handlers.errorHandler());
}
exceptionHandler() {
this.server.use(async (err, req, res, next) => {
if (process.env.NODE_ENV === 'development') {
const errors = await new Youch(err, req).toJSON();
return res.status(500).json(errors);
}
return res.status(500).json({ error: 'Internal Server Error' });
});
}
}
export default new App().server;