-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
94 lines (82 loc) · 2.72 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const config = require('./server/config');
const db_infos = require('./server/database');
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('./server/io').init(server);
const path = require('path');
const session = require('express-session');
const MySQLStore = require('express-mysql-session')(session);
const bodyParser = require('body-parser');
const userUtils = require('./server/user/utils');
const prod = (process.env.PROD == "true" || false);
const port = (process.env.PORT || 3000);
process.on('unhandledRejection', (reason, p) => {
console.error(reason, 'Unhandled Rejection at Promise', p);
})
.on('uncaughtException', err => {
console.error(err, 'Uncaught Exception thrown');
});
if (!prod) {
const webpack = require('webpack');
const webpackMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const webpackConfig = require('./webpack.config.js');
const compiler = webpack(webpackConfig);
app.use(webpackMiddleware(compiler, {
serverSideRender: true,
publicPath: webpackConfig.output.publicPath
}));
app.use(webpackHotMiddleware(compiler));
}
const routes = require('./server/routes.js');
var sqlOptions = {
host : db_infos.db_host,
port : 3306,
user : db_infos.db_user,
password : db_infos.db_pwd,
database : db_infos.db_name
}
var sessionStore = new MySQLStore(sqlOptions)
app.set('trust proxy', 1);
app.use(session({
key: 'session_cookie_name',
secret: config.secret,
resave: false,
saveUninitialized: false,
store: sessionStore,
cookie: {
maxAge: 60 * 60 * 24 * 1000
}
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
io.on('connection', socket => {
let currentUserId;
socket.on('disconnect', () => {
userUtils.setLastLogged(currentUserId);
});
socket.on('join', data => {
currentUserId = data.id;
socket.join(data.id);
});
socket.on('new_message', data => {
io.sockets.in(data.to).emit('new_message', {roomId: data.roomId});
});
socket.on('is_online', data => {
let online = io.sockets.adapter.rooms[data.userId] !== undefined ? true : false;
socket.emit('is_online', online);
});
socket.on('logout', () => {
userUtils.setLastLogged(currentUserId);
});
});
app.use('/api', routes);
app.use('/', express.static(path.join(__dirname, 'public')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public/index.html'));
});
server.listen(port, err => {
if (err) console.error(err);
console.log("App listening on port " + port + "!");
})