-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (41 loc) · 1.27 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
const express = require('express');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const mongoose = require('mongoose');
const productosRouter = require('./api/recursos/productos/productos.routes');
const usuariosRouter = require('./api/recursos/usuarios/usuarios.routes');
const logger = require('./utils/logger');
const authJWT = require('./api/libs/auth');
const config = require('./config');
const errorHandler = require('./api/libs/errorHandler');
const passport = require('passport');
passport.use(authJWT);
logger.info(config.mongo);
mongoose.connect(config.mongo);
mongoose.connection.on('error', () => {
logger.error('Falló la conexión a mongodb');
process.exit(1);
});
const app = express();
app.use(bodyParser.json());
app.use(morgan('short', {
stream: {
write: message => logger.info(message)
}
}));
app.use(passport.initialize());
app.use('/productos', productosRouter);
app.use('/usuarios', usuariosRouter);
app.use(errorHandler.procesarErroresDeDB);
if (config.ambiente === 'prod') {
app.use(errorHandler.prodErrors);
} else {
app.use(errorHandler.devErrors);
}
const server = app.listen(config.puerto, () => {
logger.info('Escuchando en el puerto 3000.');
});
module.exports = {
app,
server,
};