-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
99 lines (83 loc) · 2.5 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
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
/**
* Module dependencies.
*/
const express = require('express');
const chalk = require('chalk');
const dotenv = require('dotenv');
const path = require('path');
const passport = require('passport');
const middlewares = require('./app/http/middlewares');
const models = require("./app/models");
const globals = require('./config/globals');
const Routes = require('./routes');
const ErrorHandler = require('./app/errors/ErrorHandler');
/**
* Load environment variables from .env file, where API keys and passwords are configured.
*/
dotenv.load({ path: '.env' });
/**
* Create Express server.
*/
const app = express();
/**
* Sync Database & their Models
*/
models.sequelize.sync().then(function() {
console.log(chalk.green('Nice! Database looks fine'));
}).catch(function(err) {
console.log(chalk.red(err, "Something went wrong with the Database Update!"));
});
// Exporting globals to node environment
global['App'] = globals;
// Passport Initialisation
require('./app/utilities/passport/passport');
app.use(passport.initialize());
/**
* Express configuration.
*/
app.set('host', process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0');
app.set('port', process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
/**
* Common Middlewares.
*/
middlewares.general(app);
middlewares.requestInterceptor(app);
middlewares.responses(app);
// For adding user info in the global req object per session
middlewares.auth.userLogin(app);
// For listening to firease db events
if(process.env.TRIGGER_PUSH_NOTIFICATION) {
middlewares.firebaseService.listenToDbChanges();
}
/**
* Loading providers.
*/
require('./app/providers');
/**
* Primary app routes.
*/
console.log(chalk.black.bgBlue("Initialising routes..."));
for(let routerKey in Routes) {
console.log(chalk.blue.bold(`Loading routes for prefix: ${Routes[routerKey]['prefix']}`));
app.use(Routes[routerKey]['prefix'], Routes[routerKey]['routes']());
}
/**
* Error Handler.
*/
if (process.env.NODE_ENV === 'development') {
// only use in development
// app.use(errorHandler());
}
// Exposing directories from server
app.use("/public", express.static(path.join(__dirname, 'public')));
// Handling Errors (Global Handler)
app.use(ErrorHandler);
/**
* Start Express server.
*/
app.listen(app.get('port'), () => {
console.log('%s App is running at http://localhost:%d in %s mode', chalk.green('✓'), app.get('port'), app.get('env'));
console.log(' Press CTRL-C to stop\n');
});
module.exports = app;