-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps-server.js
More file actions
69 lines (47 loc) · 1.56 KB
/
https-server.js
File metadata and controls
69 lines (47 loc) · 1.56 KB
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
require('dotenv').config();
const HTTPS_PORT = process.env.HTTPS_PORT;
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const MONGO_URL = require('./mongoURL');
var https = require('https');
var fs = require('fs');
var hostname = '0.0.0.0'
//Load HTTPS Certificates Paths
var https_options = {
cert: fs.readFileSync("/root/cert/prj666-2021.crt"),
ca: fs.readFileSync('/root/cert/RapidSSL_RSA_CA_2018.crt'),
key: fs.readFileSync("/root/cert/prj666-2021.key"),
};
//set up express app
const app = express();
// body parsing middleware
app.use(bodyParser.json({limit: '10MB'}))
// allow CORS
app.use(cors())
// prevent 304 error
app.use(express.static(__dirname + '/static'));
// error handling middleware
app.use((err, req, res, next) => {
res.status(422).send({ error: err })
});
// listen for requests
const httpsServer = https.createServer(https_options, app);
httpsServer.listen(HTTPS_PORT, hostname);
// setup socket io
var io = require('socket.io')(httpsServer);
// require routes and pass io to them
const routes = require('./routes/index.js')(io);
// initialize routes
app.use('/api', routes);
// connect to mongodb and initialize data
const initialData = require('./initialData')(io);
mongoose.connect(MONGO_URL, { useNewUrlParser: true }).then(() => {
console.log("connected to mongoDB")
initialData.initialData();
}).catch(err => {
console.log("ERRORS: ", err);
console.log("MONGODB CONNECTION FAILED.")
// process.exit();
});