forked from dekkerglen/CubeCobra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
137 lines (115 loc) · 3.35 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const express = require('express');
// eslint-disable-next-line import/no-extraneous-dependencies
const path = require('path');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const session = require('express-session');
const passport = require('passport');
const http = require('http');
const fileUpload = require('express-fileupload');
const MongoDBStore = require('connect-mongodb-session')(session);
const schedule = require('node-schedule');
// eslint-disable-next-line import/no-unresolved
const secrets = require('../cubecobrasecrets/secrets');
// eslint-disable-next-line import/no-unresolved
const mongosecrets = require('../cubecobrasecrets/mongodb');
const updatedb = require('./serverjs/updatecards.js');
const carddb = require('./serverjs/cards.js');
carddb.initializeCardDb();
// Connect db
mongoose.connect(mongosecrets.connectionString);
const db = mongoose.connection;
db.once('open', () => {
console.log('connected to nodecube db');
});
// Check for db errors
db.on('error', (err) => {
console.log(err);
});
// Init app
const app = express();
const store = new MongoDBStore(
{
uri: mongosecrets.connectionString,
databaseName: mongosecrets.dbname,
collection: 'session_data',
},
(err) => {
if (err) {
console.log(`store failed to connect to mongoDB:\n${err}`);
}
},
);
// upload file middleware
app.use(fileUpload());
// Body parser middleware
app.use(
bodyParser.urlencoded({
limit: '50mb',
extended: true,
}),
);
app.use(
bodyParser.json({
limit: '50mb',
extended: true,
}),
);
// Load view engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// Set Public Folder
app.use(express.static(path.join(__dirname, 'public')));
app.use('/js', express.static(path.join(__dirname, 'dist')));
app.use('/jquery-ui', express.static(`${__dirname}/node_modules/jquery-ui-dist/`));
const sessionOptions = {
secret: secrets.session,
store,
resave: true,
saveUninitialized: true,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7, // 1 week
},
};
if (secrets.environment === 'production') {
app.set('trust proxy', 1);
sessionOptions.cookie.secure = true;
}
// Express session middleware
app.use(session(sessionOptions));
// Express messages middleware
app.use(require('connect-flash')());
app.use((req, res, next) => {
res.locals.messages = require('express-messages')(req, res);
res.locals.node_env = app.get('env');
next();
});
// Passport config and middleware
require('./config/passport')(passport);
app.use(passport.initialize());
app.use(passport.session());
app.get('*', (req, res, next) => {
res.locals.user = req.user || null;
next();
});
// Route files; they manage their own CSRF protection
const cubes = require('./routes/cube_routes');
const users = require('./routes/users_routes');
const devs = require('./routes/dev_routes');
const tools = require('./routes/tools_routes');
app.use('', require('./routes/root'));
app.use('/cube', cubes);
app.use('/user', users);
app.use('/dev', devs);
app.use('/tool', tools);
app.use((req, res) => {
res.status(404).render('misc/404', {});
});
schedule.scheduleJob('0 0 * * *', () => {
console.log('Starting midnight cardbase update...');
updatedb.updateCardbase();
});
// Start server
http.createServer(app).listen(5000, 'localhost', () => {
console.log('server started on port 5000...');
});