-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·71 lines (50 loc) · 1.6 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
const express = require('express');
const fs = require('fs');
const { join } = require('path');
const passport = require('passport');
const logger = require('mean-logger');
const io = require('socket.io');
const auth = require('./config/middlewares/authorization');
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const app = express();
/**
* Main application entry file.```
* Please note that the order of loading is important.
*/
// Load configurations
// if test env, load example file
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
const config = require('./config/config');
// Bootstrap db connection
mongoose.connect(config.db);
// Bootstrap models
const modelsPath = join(__dirname, '/app/models');
const walk = function (path) {
return fs.readdirSync(path).forEach((file) => {
const newPath = `${path}/${file}`;
const stat = fs.statSync(newPath);
if (stat.isFile()) {
if (/(.*)\.(js|coffee)/.test(file)) {
require(newPath);
}
} else if (stat.isDirectory()) {
walk(newPath);
}
});
};
walk(modelsPath);
// express settings
require('./config/express')(app, passport, mongoose);
require('./config/passport')(passport);
require('./config/routes')(app, passport, auth);
// Start the app by listening on <port>
const server = app.listen(config.port);
const ioObj = io.listen(server, { log: false });
// game logic handled here
require('./config/socket/socket')(ioObj);
console.log(`Express app started on port ${config.port}`);
// Initializing logger
logger.init(app, passport, mongoose);
// expose app
module.exports = app;