-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
194 lines (158 loc) · 4.75 KB
/
server.js
File metadata and controls
194 lines (158 loc) · 4.75 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
var stormpath = require('express-stormpath');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var path = require('path');
var express = require('express');
var webpack = require('webpack');
var config = require('./webpack.config');
var ora = require('ora');
var dbFunc = require('./model/database.js');
var port = process.env.PORT || 3000;
var app = express();
var compiler = webpack(config);
var spinner = ora({
interval: 100
});
function failAndExit(err) {
spinner.fail();
console.error(err.stack);
process.exit(1);
}
app.use(express.static(__dirname + '/public/'));
app.use(morgan('combined'));
app.use(bodyParser.json());
app.use(require('webpack-dev-middleware')(compiler, {
filename: 'bundle.js',
hot: true,
stats: {
colors: true,
},
historyApiFallback: true,
publicPath: config.output.publicPath
}));
app.use(require('webpack-hot-middleware')(compiler, {
log: console.log,
path: '/__webpack_hmr',
heartbeat: 10 * 1000
}));
app.use('/css', express.static(__dirname + '/src/css'));
app.use(stormpath.init(app, {
// Disable logging until startup, so that we can catch errors
// and display them nicely.
debug: 'none',
web: {
produces: ['application/json'],
me: {
expand: {
customData: true
}
},
register: {
form: {
fields: {
color: {
enabled: true,
label: 'Color',
placeholder: 'E.g. blue',
type: 'text'
}
}
}
}
}
}));
app.get('/home/:email', function (req, res) {
console.log("MIDDLE::HOME USER EMAIL"+ JSON.stringify(req.params.email));
dbFunc.verifyUser(req.params.email, function (data, err) {
if (data) {
console.log("MIDDLE::RETURNED SQL - "+JSON.stringify(data))
res.status(200).send(data);
}else {
console.log("GETFAILED");
res.status(500).send('fail');
}
})
})
app.get('/eventers/:tier', function (req, res) {
console.log("MIDDLE::GET TIER 1 EVENTERS CALLED "+ JSON.stringify(req.params.tier));
dbFunc.getEventers(req.params.tier, function (data, err) {
if (data) {
console.log("MIDDLE::TIER 1 EVENTERS "+JSON.stringify(data))
res.status(200).send(data);
}else {
console.log("MIDDLE::GETFAILED FOR TIER 1 EVENTERS");
res.status(500).send('fail');
}
})
})
app.post('/eventerteam/', function (req, res) {
console.log("MIDDLE::SUBMIT EVENT TEAM CALLED::ID"+ (req.body.id));
console.log("MIDDLE::SUBMIT EVENT TEAM CALLED::teamName "+ (req.body.teamName));
console.log("MIDDLE::SUBMIT EVENT TEAM CALLED::team "+ JSON.stringify(req.body.team));
dbFunc.submitEventTeam(req.body.id,req.body.teamName, function (data, err) {
if (data) {
console.log("MIDDLE::INSERTED EVENT TEAM "+JSON.stringify(data))
res.status(200).send(data);
}else {
console.log("MIDDLE::FAILED::INSERTED EVENT TEAM");
res.status(500).send('fail');
}
})
})
app.post('/me', stormpath.loginRequired, function (req, res) {
function writeError(message) {
res.status(400);
res.json({ message: message, status: 400 });
res.end();
}
function saveAccount() {
req.user.givenName = req.body.givenName;
req.user.surname = req.body.surname;
req.user.email = req.body.email;
console.log("REQ USER FROM /ME POST1: "+req.user);
if ('color' in req.body.customData) {
req.user.customData.color = req.body.customData.color;
}
req.user.save(function (err) {
if (err) {
return writeError(err.userMessage || err.message);
}
console.log("REQ USER FROM /ME POST2: "+req.user);
res.end();
});
}
if (req.body.password) {
var application = req.app.get('stormpathApplication');
application.authenticateAccount({
username: req.user.username,
password: req.body.existingPassword
}, function (err) {
if (err) {
return writeError('The existing password that you entered was incorrect.');
}
req.user.password = req.body.password;
saveAccount();
});
} else {
console.log("REQ USER FROM /ME POST3: "+req.user);
saveAccount();
}
});
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, 'src/html/index.html'));
});
spinner.text = 'Starting Dev Sever on port ' + port,
spinner.start();
app.on('error', failAndExit);
app.on('stormpath.error', failAndExit);
app.listen(port, function () {
spinner.succeed();
spinner.text = 'Initializing Stormpath';
spinner.start();
app.on('stormpath.ready', function () {
spinner.succeed();
console.log('\nListening at http://localhost:' + port);
// Now bring back error logging.
app.get('stormpathLogger').transports.console.level = 'error';
});
});