-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
48 lines (48 loc) · 1.82 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
const fs = require('fs');
const https = require('https');
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const expressValidator = require('express-validator');
const path = require('path');
const port = process.env.PORT || 443;
const httpsOptions = {
key: fs.readFileSync('./file.pem'),
cert: fs.readFileSync('./file.crt')
};
const server = https.createServer(httpsOptions, app);
const io = require('socket.io')(server);
const modularIo = require('./client/io.js')(io);
/*
* Mongoose by default sets the auto_reconnect option to true.
* We recommend setting socket options at both the server and replica set level.
* We recommend a 30 second connection timeout because it allows for
* plenty of time in most operating environments.
*/
const options = { server: { socketOptions: { keepAlive: 300000, connectTimeoutMS: 30000 } },
replset: { socketOptions: { keepAlive: 300000, connectTimeoutMS : 30000 } } };
/* mongodb://vote:[email protected]:33582/vote */
/* mongodb://localhost:27017/vote */
let mongodbUri = 'mongodb://localhost:27017/vote';
mongoose.connect(mongodbUri, options);
let conn = mongoose.connection;
conn.on('error', console.error.bind(console, 'connection error:'));
let initHttpServer = () => {
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static('public'));
app.use(expressValidator());
app.use(require('./routes'));
app.set('views', path.join(__dirname, './client'));
app.set('view engine', 'ejs');
app.get('/', (req,res) => { res.render('index'); });
server.listen(port, function() {
console.log('express listening on port' + port);
});
return app;
}
conn.once('open', function() {
initHttpServer();
});
module.exports = initHttpServer;