-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
executable file
·71 lines (64 loc) · 2.15 KB
/
server.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
'use strict';
const express = require('express'),
app = express(),
fs = require('fs'),
socketServer = require('http').Server(app),
io = require('socket.io')(socketServer),
bodyParser = require('body-parser'),
session = require('express-session'),
MongoStore = require('connect-mongo')(session),
path = require('path'),
MongoUrl = 'mongodb://merlox:[email protected]:45750/merunas',
apiRoutes = require('./server/routes/apiRoutes.js'),
adminRoutes = require('./server/routes/adminRoutes.js'),
publicRoutes = require('./server/routes/publicRoutes.js'),
socketRoutes = require('./server/routes/socketRoutes.js');
const yargs = require('yargs')
const argv = yargs.option('port', {
alias: 'p',
description: 'Set the port to run this server on',
type: 'number',
}).help().alias('help', 'h').argv
if(!argv.port) {
console.log('Error, you need to pass the port you want to run this application on with npm start -- -p 8001')
process.exit(0)
}
const port = argv.port
let username = null
//Inicializamos configuracion de express y sessión
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(session({
secret: 'GOODPASSWORD',
store: new MongoStore({
url: MongoUrl
}),
cookie: {
//Un mes
maxAge: 1000 * 60 * 60 * 24 * 30
},
resave: true,
unset: 'destroy',
saveUninitialized: true
}));
//Para cargar los archivos publicamente usamos
app.use(express.static(path.join(__dirname, 'public')));
//Logger middleware
app.use('*', (req, res, next) => {
let time = new Date()
console.log(`${req.method} to ${req.originalUrl} at ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`)
if(req.session.username) username = req.session.username;
// req.session.cesta = [];
// console.log(req.session)
next();
});
//Custom routes
app.use('/api', apiRoutes);
app.use('/admin', adminRoutes);
app.use('/', publicRoutes);
//Ejecutamos el servidor en la ip local
socketServer.listen(port, '0.0.0.0', function(){
console.log("Server started at localhost:", port);
});
//Ejecutamos el servidor socket.io para el chat en realtiime
socketRoutes(io, username);