-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
152 lines (128 loc) · 3.62 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const express = require('express');
const app = express();
const cors = require('cors');
const morgan = require('morgan');
const config = require('config');
const serverEnv = process.env.NODE_ENV || 'dev';
let apiBasePath = config.get('server.apiBaseRoot') || '/api';
const FileStreamRotator = require('file-stream-rotator');
let logDirectory = __dirname + '/logs';
const fs = require('fs');
const http = require('http').Server(app);
const Core = require('./server/classes/core');
const session = require('express-session');
const MongoStore = require('connect-mongo');
const helmet = require('helmet');
const mongoSanitize = require('express-mongo-sanitize');
// const { rateLimit } = require('express-rate-limit');
/**
* Connect to MongoDB
*/
const db = Core.dbConnect();
app.db = db;
app.use(helmet({
contentSecurityPolicy: false,
crossOriginEmbedderPolicy: false,
}));
/**
* Sanitize MongoDB request
*/
app.use(mongoSanitize());
/**
* Add compression
*/
let shouldCompress = (req, res) => {
if (req.headers['x-no-compression']) {
// don't compress responses with this request header
return false
}
// fallback to standard filter function
return compression.filter(req, res)
};
/**
* Session storage, in production it should be saved in a MongoDB Collection
*/
const sess = {
secret: config.get('security.secret'),
cookie: {},
store: MongoStore.create({
autoRemove: 'interval',
autoRemoveInterval: 10, // In minutes. Default
collection: 'sessions',
mongoUrl: Core.getConnectionString()
}),
resave: true,
saveUninitialized: true
}
if (serverEnv === 'production') {
// Compression
app.use(compression({filter: shouldCompress}))
// Sessions
app.set('trust proxy', 1); // trust first proxy
sess.cookie.secure = config.get('server.sslEnabled'); // serve secure cookies
}
app.use(session(sess));
app.use(express.json());
/**
* Set static files location
* used for requests that our frontend will make
*/
// app.use('/static', express.static(__dirname + '/static'));
app.use(`${apiBasePath}/docs`, express.static(__dirname + '/apidoc'));
/**
* Allow CORS
*/
app.use(cors());
if(serverEnv === 'production'){
// ensure log directory exists
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory);
// create a rotating write stream
var accessLogStream = FileStreamRotator.getStream({
date_format: 'YYYYMMDD',
filename: logDirectory + '/access-%DATE%.log',
frequency: 'daily',
verbose: false
});
// log all the request on the console
app.use(morgan('combined', {stream: accessLogStream}));
}// end if
if(serverEnv !== 'production'){
app.use(morgan('dev'));
}// end if
app.get(apiBasePath+'/version', Core.noCache, function(req, res){
var version = require('./package.json').version;
var data = {
version
}
res.json(data);
});
/**
* API routes
*/
const usersApiRouter = require('./server/routes/users')(app, express);
/**
* Assign the API routes to the main app
*/
app.use(apiBasePath, usersApiRouter);
/**
* To add a rate limit
*/
// const limit = rateLimit({
// max: 100,// max requests
// windowMs: 60 * 60 * 1000, // 1 Hour
// message: 'Too many requests' // message to send
// });
// app.use('/routeName', limit);
// app.get('/*', Core.noCache, function ( req, res ) {
// res
// .status( 200 )
// .set({ 'content-type': 'text/html; charset=utf-8' })
// .sendFile( __dirname + '/public/index.html' )
// });
/**
* Start the server
*/
http.listen(config.get('server.port'), config.get('server.host'), function(){
console.log(`Server started at the address ${config.get('server.host')}:${config.get('server.port')}`);
})
module.exports = app;