-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
134 lines (101 loc) · 3.53 KB
/
server.js
File metadata and controls
134 lines (101 loc) · 3.53 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require('dotenv').load();
// Modules =================================================
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const passport = require('passport');
const morgan = require('morgan');
const logger = require('./server/log/logger');
const fs = require('fs');
const helmet = require('helmet');
const formidable = require('express-formidable');
//MAIN Env. variables, needed to the project to run
const node_env = process.env.NODE_ENV;
const DB_SECRET = process.env.DB_SECRET;
const DB_PRODUCTION = process.env.DB_PRODUCTION;
const mongoose = require('mongoose');
const Utils = require('./server/mongodb/accesses/utils-accesses');
// Configuration ===========================================
let appConfig = require('./config/app');
let port = process.env.PORT || appConfig.ports[node_env];
let dbConfig = require('./config/db');
mongoose.connect(dbConfig.urls[node_env], { useNewUrlParser: true, keepAlive: 1, connectTimeoutMS: 30000,
keepAlive: 1, connectTimeoutMS: 30000 });
mongoose.connection.on("connected", () => {
logger.info("Connected to MongoDB on " + dbConfig.urls[node_env]);
});
mongoose.connection.on("error", (dbError) => {
logger.error("Could not connect to database on: " + dbConfig.urls[node_env]);
throw new Error(dbError);
});
mongoose.set('useCreateIndex', true);
const NODE_ENV_ERROR =
"[ERROR] - NODE_ENV not set.\n" +
"Define it on your environment variables.";
if (!node_env) {
return console.error(NODE_ENV_ERROR);
}
logger.info("NODE_ENV = " + node_env);
if (DB_SECRET) {
logger.info("DB_SECRET IS " + "DEFINED");
} else {
logger.warn("DB_SECRET IS NOT " + "NOT DEFINED");
}
if(!DB_PRODUCTION && node_env === "production") {
console.error("Production DB not set. Please export the environment variable");
}
//Initialize app
const app = express();
// Body Parser Middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Passport Middleware
app.use(passport.initialize());
app.use(passport.session());
require('./config/passport')(passport);
// enable CORS without external module
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "*");
res.header('Access-Control-Allow-Methods', '*');
if ('OPTIONS' === req.method) {
//respond with 200
}
next();
});
app.use(formidable());
//Summarized version, output goes to console
app.use(morgan('dev'));
//Full log to file
app.use((morgan)("common", {stream: logger.stream}));
// Security ===========================================
//Defaults:
helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'"],
styleSrc: ["'self'", 'maxcdn.bootstrapcdn.com']
}
});
app.use(helmet({
}));
// Set Static Folder
app.use(express.static(path.join(__dirname, 'public')));
// Routes
require('./server/routes')(app, __dirname);
process.on('uncaughtException', (err) => {
switch(err.errno) {
case "EADDRINUSE":
logger.error("Address in use. Port: being used:" + port);
break;
default:
fs.writeSync(1, `Caught exception: ${err}\n`);
logger.error("Process terminating: \n");
logger.error(err);
}
process.exit();
});
//We use Heroku, so we delegate SSL implementation to Heroku's load balancer.
app.listen(port, () => {
logger.info("Server running on port: " + port);
logger.warn("Server running since: " + new Date());
});