-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
72 lines (65 loc) · 2.29 KB
/
app.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
// -------------------------- Requirements ----------------------------- //
require('./api/config/DBConnection');
var config = require('./api/config/config');
var cors = require('cors');
var express = require('express');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var compression = require('compression');
var bodyParser = require('body-parser');
var helmet = require('helmet');
var passport = require('passport');
var path = require('path');
require('./api/config/passport')(passport);
var router = require('./api/routes/index')(passport);
// -------------------------- End of "Requirements" --------------------- //
// -------------------------- Dependancies ------------------------------ //
var app = express();
app.set(config.SECRET);
// -------------------------- End of "Dependancies" --------------------- //
// Create link to Angular build directory
var distDir = path.join(__dirname, '/dist/');
app.use(express.static(distDir));
// -------------------------- Middleware -------------------------------- //
app.use(cors({
credentials: true,
origin: true
}));
app.use(helmet());
app.use(compression());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(passport.initialize());
app.use(passport.session());
app.use('/api', router);
// -------------------------- End of "Middleware" ----------------------- //
// -------------------------- Error Handlers ---------------------------- //
app.get('/*', function(req, res) {
res.sendFile('index.html', { root: distDir });
});
// 500 internal server error handler
app.use(function (err, req, res, next) {
if (err.statusCode === 404) {
return next();
}
res.status(500).json({
// Never leak the stack trace of the err if running in production mode
data: null,
err: config.ENV === 'prod' ? null : err,
msg: '500 Internal Server Error'
});
});
// 404 error handler
app.use(function (req, res) {
res.status(404).json({
data: null,
err: null,
msg: '404 Not Found'
});
});
// -------------------------- End of "Error Handlers" ------------------- //
// -------------------------- Exports ----------------------------------- //
module.exports = app;
// -------------------------- End of "Exports" -------------------------- //