-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (51 loc) · 1.65 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
require('dotenv').config()
const User = require('./models/user.model')
const createError = require('http-errors')
const express = require('express')
const logger = require('morgan')
const mongoose = require('mongoose')
require('./config/db.config')
const session = require('./config/session.config')
const auth = require('./middlewares/auth.middleware')
const app = express()
// Middlewares
app.use(logger('dev'))
app.use(express.json())
app.use(session)
app.use(auth.loadUser)
//Routes
const routes = require('./config/routes.config')
app.use('/api', routes)
// Error handling
app.use((req, res, next) => {
next(createError(404, 'Route not found'))
})
app.use((error, req, res, next) => {
if (error instanceof mongoose.Error.ValidationError) {
error = createError(400, error);
} else if (error instanceof mongoose.Error.CastError && error.message.includes('_id')) {
error = createError(404, 'Resource not found');
} else if (error.message.includes('E11000')) {
error = createError(409, 'Duplicated');
//409 recurso duplicado error conflict
} else if (!error.status) {
error = createError(500, error);
}
if (error.status >= 500) {
console.error(error);
}
const data = {};
data.message = error.message;
if (error.errors) {
data.errors = Object.keys(error.errors)
.reduce((errors, key) => {
errors[key] = error.errors[key].message;
return errors;
}, {});
}
res.status(error.status).json(data);
});
const port = process.env.PORT || 8000
app.listen(port, () => {
console.info(`Application running. Port: ${port}`)
})