-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·137 lines (111 loc) · 3.64 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const express = require('express'),
mongoose = require('mongoose'),
cloudinary = require('cloudinary'),
session = require('express-session'),
MongoDBStore = require('connect-mongodb-session')(session),
morgan = require('morgan'),
favicon = require('serve-favicon'),
path = require('path'),
app = express();
//Configuración
app.set('port', process.env.PORT || 3000);
// Permite utilizar environment variables
const dotenv = require('dotenv');
dotenv.config();
//Inicialización del servidor
app.listen(app.get('port'), () => {
console.log('El servidor esta corriendo en el puerto', app.get('port'));
});
//Conecta la aplicación con la base de datos
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true
},
() => {
console.log('La base de datos fue conectada exitosamente!')
});
mongoose.set('useCreateIndex', true);
//Messaging system
const io = require('socket.io')(5000)
const users = {}
io.on('connection', socket => {
socket.on('new-user', name => {
users[socket.id] = name
socket.broadcast.emit('user-connected', name)
})
socket.on('send-chat-message', message => {
socket.broadcast.emit('chat-message', { message: message, name: users[socket.id] })
})
socket.on('disconnect', () => {
socket.broadcast.emit('user-disconnected', users[socket.id])
delete users[socket.id]
})
})
//Se encarga de los errores CORS
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Acess-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
if (req.method === 'OPTIONS') {
res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET');
return res.status(200).json({
message: 'It works!'
});
}
next();
});
//Envia el favicon en todo request
app.use(favicon(path.join(__dirname, 'public/images/', 'favicon.ico')));
// Permite subir las imagenes a la nube
cloudinary.config({
cloud_name: process.env.CLOUDINARY_NAME,
api_key: process.env.CLOUDINARY_KEY,
api_secret: process.env.CLOUDINARY_SECRET
});
// Muestra en consola los HTTP requests que se hacen al servidor
app.use(morgan('dev'));
// Middleware and Body-parser
app.use(express.urlencoded({extended: false}));
app.use(express.json());
// Sessions
app.use(session({
store: new MongoDBStore({
uri: process.env.MONGO_URI,
collection: 'sessions'
}),
cookie: {
maxAge: 1000 * 60 * 60 * 2,
sameSite: true,
httpOnly: false
},
name: process.env.SESSION_NAME,
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
}));
//Establecer la ruta para archivos estáticos
app.use(express.static('public'));
app.use(express.static('/public/uploads'));
//Importación de las rutas
const usuario = require('./api/components/usuario/routes');
const libreria = require('./api/components/libreria/routes');
const sucursal = require('./api/components/sucursal/routes');
const club = require('./api/components/club/routes');
const oferta = require('./api/components/oferta/routes');
const autor = require('./api/components/autor/routes');
const libro = require('./api/components/libro/routes');
const search = require('./api/components/search/routes');
//Rutas
app.use('/usuario', usuario);
app.use('/libreria', libreria);
app.use('/sucursal', sucursal);
app.use('/club', club);
app.use('/oferta', oferta);
app.use('/autor', autor);
app.use('/libro', libro);
app.use('/search', search);
app.get('/', (req, res) => {
res.sendFile('landing-page.html', {root: 'public'});
});
app.get('/landing-page', (req, res) => {
res.sendFile('landing-page.html', {root: 'public'});
});
module.exports = app;