-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
114 lines (102 loc) · 3.27 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require('dotenv').config({path: './config/.env'});
const app = require('express')();
const db = require('./config/db');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const cors = require('cors');
const Sentry = require('@sentry/node');
const Tracing = require('@sentry/tracing');
const userRoutes = require('./routes/user.routes');
const cdcRoutes = require('./routes/cdc.routes');
const corsOptions = {
origin: [
`${process.env.CLIENT_URL}`,
'http://localhost:3000',
'http://192.168.1.30:3000',
],
credentials: true,
allowedHeaders: [
'sessionId',
'Content-Type',
'Authorization',
'authorization',
'baggage',
'sentry-trace',
],
exposedHeaders: [ 'sessionId' ],
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
};
app.use(cors(corsOptions));
db.init();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
// Routes
app.use('/api/user', userRoutes);
app.use('/api/cdc', cdcRoutes);
app.listen(process.env.PORT, () => {
console.log(`Server started http://localhost:${process.env.PORT}`);
});
Sentry.init({
dsn: process.env.SENTRY_DSN,
integrations: [
// enable HTTP calls tracing
new Sentry.Integrations.Http({tracing: true}),
// enable Express.js middleware tracing
new Tracing.Integrations.Express({app}),
],
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
});
// RequestHandler creates a separate execution context using domains, so that every
// transaction/span/breadcrumb is attached to its own Hub instance
app.use(Sentry.Handlers.requestHandler());
// TracingHandler creates a trace for every incoming request
app.use(Sentry.Handlers.tracingHandler());
// The error handler must be before any other error middleware and after all controllers
app.use(Sentry.Handlers.errorHandler());
// Optional fallthrough error handler
app.use(function onError(err, req, res, next){
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500;
res.end(res.sentry + '\n');
});
//Check error
process.on('exit', (code) => {
console.log(
`${new Date(Date.now())
.toLocaleString('fr-FR', {
timeZone: 'Europe/Paris',
})
.toString()} --> Le processus s'est arrêté avec le code: ${code}!`,
);
});
process.on('uncaughtException', (err, origin) => {
console.log(
`${new Date(Date.now())
.toLocaleString('fr-FR', {timeZone: 'Europe/Paris'})
.toString()} --> [uncaughtException] ${err}\n------------------------------------------\n${origin}\n------------------------------------------`,
);
});
process.on('unhandledRejection', (reason, promise) => {
console.log(
`${new Date(Date.now())
.toLocaleString('fr-FR', {timeZone: 'Europe/Paris'})
.toString()} --> [UNHANDLED_REJECTION] ${reason}\n------------------------------------------\n${promise}\n------------------------------------------`,
);
console.log(promise);
});
process.on('warning', (...args) => {
console.log(
`${new Date(Date.now())
.toLocaleString('fr-FR', {
timeZone: 'Europe/Paris',
})
.toString()} --> [WARNING]\n------------------------------------------\n`,
...args,
);
});