-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
43 lines (40 loc) · 1.53 KB
/
app.js
File metadata and controls
43 lines (40 loc) · 1.53 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
var port = process.env.PORT || 3000,
express = require('express'),
app = express(),
path = require("path"),
bodyParser = require('body-parser'),
urlencodedParser = bodyParser.urlencoded({ extended:false }),
pool = require('./pool');
app.use(express.static(path.join(__dirname + '/public')));
app.use(bodyParser.json());
var classes = ["Barbarian", "Bard", "Cleric", "Druid", "Fighter", "Monk", "Paladin", "Ranger",
"Rogue", "Sorcerer", "Warlock", "Wizard"];
app.post('/', urlencodedParser, function(req,res) {
if (req.body.level !== undefined) {
var level = req.body.level;
if (isNaN(level) || level % 1 !== 0) { //is not a number or is not an integer
res.send("Error: Level is not a number");
}
else if (level === "" || level < 1 || level > 19) {
res.send("Error: Level should be between 1-19")
}
else if (!classes.includes(req.body.class)) {
res.send("Error: You must select a class");
}
else {
pool.query('CALL level_up(?,?)',[req.body.class, parseInt(level)+1], function(err, rows, fields) {
if (err) throw err;
res.send(rows[0][0]);
});
}
}
else if (req.body.feature !== undefined) {
pool.query('CALL get_feature(?)',req.body.feature, function(err, rows, fields) {
if (err) throw err;
res.send(rows[0][0]);
});
}
});
app.listen(port, function() {
console.log('Server running at http://some_host:' + port + '/');
});