-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (78 loc) · 2.21 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
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
require('dotenv').config()
const bodyParser = require("body-parser");
const express = require("express");
const Sentry = require("@sentry/node");
const Tracing = require("@sentry/tracing");
const router = require("./routes/routes");
const morgan = require("morgan");
const mongoose = require('mongoose');
const CustomError = require("./models/error.model");
const app = express();
app.use(bodyParser.json({limit: '50mb'}));
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "*");
res.header("Access-Control-Allow-Methods", "*");
next();
});
Sentry.init({
dsn: process.env.SENTRY_URL,
integrations: [
new Sentry.Integrations.Http({ tracing: true }),
new Tracing.Integrations.Express({ app }),
],
tracesSampleRate: 1.0,
});
mongoose.connect(
process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
},
(err) => {
if (err) throw err;
console.log("👍 Conectado correctamente a la base de datos");
}
);
app.use(Sentry.Handlers.requestHandler());
app.use(Sentry.Handlers.tracingHandler());
// @ts-ignore
String.prototype.toTitleCase = function () {
return this.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
};
app.use(morgan("dev"));
// @ts-ignore
app.get("/", (req, res) => {
res.json({
funcionando: true,
estado: "API funcionando correctamente",
});
});
app.use("/v1", router);
app.use(Sentry.Handlers.errorHandler());
// @ts-ignore
app.use(function onError(err, req, res, next) {
console.error(err);
if (err instanceof CustomError) {
res.statusCode = err.statusCode;
res.json({
codigoHttp: err.statusCode ? err.statusCode : 500,
mensaje: err.message ? err.message : "Error inesperado",
codigoInterno: err.internalCode ? err.internalCode : 0,
error: err.toString(),
});
} else {
res.statusCode = 500;
res.json({
codigoHttp: 500,
mensaje: "Error inesperado",
codigoInterno: 0,
error: err.toString(),
});
}
});
let puerto = process.env.PORT || 3100;
app.listen(puerto, () => {
console.log("🚀 Escuchando en el puerto", puerto);
});