-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
50 lines (43 loc) · 1.46 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
const express = require('express');
const app = express();
const cookieParser = require('cookie-parser');
const fs = require('fs');
const https = require('https');
const db = require('./server/db/mongo');
//Constants
const PORT = process.env.PORT || 4000;
const MONGO_URI = process.env.MONGO_URI;
const PRODUCTION = process.env.NODE_ENV === 'production';
const SALT = process.env.SALT;
app.use(express.static('static'));
app.use('/modules', express.static('node_modules'));
app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.use(cookieParser());
require('./server/auth/authRoutes')(app);
require('./server/user/userRoutes')(app);
require('./server/notifications/notificationRoutes')(app);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/static/index/index.html');
});
//ENVIRONMENT VARIABLES CHECK
if (!MONGO_URI) {
throw new Error('MONGO_URI environment variable is not defined');
}
if (!SALT) {
throw new Error('SALT environment variable is not defined');
}
if (PRODUCTION) {
https.createServer({
key: fs.readFileSync('keys/key.pem'),
cert: fs.readFileSync('keys/cert.pem')
}, app).listen(PORT, () => {
console.log(`Server is running in production mode at https://localhost:${PORT}`);
db.connectToMongo(MONGO_URI);
});
} else {
app.listen(PORT, () => {
console.log(`Server is running in development mode at http://localhost:${PORT}`);
db.connectToMongo(MONGO_URI);
});
}