-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
118 lines (95 loc) · 3.17 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*jshint esversion: 6 */
/*jslint node: true */
'use strict';
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const expressValidator = require('express-validator');
const methodOverride = require('method-override');
const async = require('async');
//Schemas
const ItemSchema = require('./models/item.js');
const UserSchema = require('./models/user.js');
const CharSchema = require('./models/char.js');
const RaceSchema = require('./models/race.js');
const SkillSchema = require('./models/skill.js');
const InvetorySchema = require('./models/inventory.js');
const CharSkillsSchema = require('./models/charskills.js');
//Configurations
const config = require('./config/config.json');
const race = require('./config/races');
async.eachOfLimit(race, 1, (value, key, cb) => {
let basicAttributes = value.basicAttributes;
RaceSchema.findOneAndUpdate({name: key}, {basicAttributes: basicAttributes}, {new: true})
.exec((err, race) => {
console.log(race);
if(err || !race)
return cb('err');
return cb();
});
}, (err) => {
if(err)
return console.log(`Error starting races`);
return console.log(`All races started`);
});
//Middlewares
const routesMiddleware = require('./middlewares/errorRoutesMiddlewares.js');
//Routes
const users = require('./routes/users');
const chars = require('./routes/chars');
const races = require('./routes/races');
const app = express();
//Socket.io
const server = require('http').Server(app);
const io = require('socket.io')(server);
//Mongoose
mongoose.connect('mongodb://localhost/nodegame');
//Allowing Cross Domain
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
};
//config
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json({type: 'application/vnd.api+json'}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(allowCrossDomain);
app.use(cookieParser());
app.use(expressValidator());
app.use(require('express-status-monitor')());
app.use(methodOverride('X-HTTP-Method-Override'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(require('express-session')({
secret: 'nodegameasdasasdsadssa',
resave: true,
saveUninitialized: true
}));
app.get('/', function(req, res) {
res.render('index', {
title: 'Express'
});
});
app.use(users);
app.use(chars);
app.use('/races', races);
require('./routes/io')(io);
// error 404
app.use(routesMiddleware.notFound);
// error 500
app.use(routesMiddleware.serverError);
server.listen(8888);
app.listen(process.env.NODE_ENV === 'production' ? 80 : config.port, () => {
console.log(config.name + ' initialized on port ' + config.port);
});