-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
130 lines (118 loc) · 3.49 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
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
var express = require('express');
var d3lb = require('./client');
var app = express();
app.param('team', function(req, res, next, id) {
if (req.params.team != '2' && req.params.team != '3' && req.params.team != '4') {
endReq(req, res, "invalid team");
} else {
next();
}
});
app.param('era', function(req, res, next, id) {
if (req.params.era != '1' && req.params.era != '2' && req.params.era != '3' && req.params.era != '4' && req.params.era != '5') {
endReq(req, res, "invalid era");
} else {
next();
}
});
app.param('class', function(req, res, next, id) {
switch(req.params.class) {
case 'barbarian':
req.params.class = '1';
next();
break;
case 'monk':
req.params.class = '2';
next();
break;
case 'demon-hunter':
req.params.class = '3';
next();
break;
case 'witch-doctor':
req.params.class = '4';
next();
break;
case 'wizard':
req.params.class = '5';
next();
break;
case 'crusader':
req.params.class = '6';
next();
break;
default:
endReq(req, res, "invalid class");
}
});
app.get('/season/:era/class/:class', function(req, res) {
console.log('serving /season/'+req.params.era+"/class/"+req.params.class);
d3lb.SeasonalClass(req.params.era, req.params.class, function(err, json) {
reqCallback(req, res, err, json);
});
});
app.get('/era/:era/class/:class', function(req, res) {
console.log('serving /era/'+req.params.era+"/class/"+req.params.class);
d3lb.Class(req.params.era, req.params.class, function(err, json) {
reqCallback(req, res, err, json);
});
});
app.get('/season/:era/team/:team', function(req, res) {
console.log('serving /season/'+req.params.era+"/team/"+req.params.team);
d3lb.SeasonalTeam(req.params.era, req.params.team, function(err, json) {
reqCallback(req, res, err, json);
});
});
app.get('/era/:era/team/:team', function(req, res) {
console.log('serving /era/'+req.params.era+"/team/"+req.params.team);
d3lb.Team(req.params.era, req.params.team, function(err, json) {
reqCallback(req, res, err, json);
});
});
app.get('/season/hardcore/:era/class/:class', function(req, res) {
console.log('serving /season/hardcore/'+req.params.era+"/class/"+req.params.class);
d3lb.SeasonalHardcoreClass(req.params.era, req.params.class, function(err, json) {
reqCallback(req, res, err, json);
});
});
app.get('/era/hardcore/:era/class/:class', function(req, res) {
console.log('serving /era/hardcore/'+req.params.era+"/class/"+req.params.class);
d3lb.HardcoreClass(req.params.era, req.params.class, function(err, json) {
reqCallback(req, res, err, json);
});
});
app.get('/season/hardcore/:era/team/:team', function(req, res) {
console.log('serving /season/hardcore/'+req.params.era+"/team/"+req.params.team);
d3lb.SeasonalHardcoreTeam(req.params.era, req.params.team, function(err, json) {
reqCallback(req, res, err, json);
});
});
app.get('/era/hardcore/:era/team/:team', function(req, res) {
console.log('serving /era/hardcore/'+req.params.era+"/team/"+req.params.team);
d3lb.HardcoreTeam(req.params.era, req.params.team, function(err, json) {
reqCallback(req, res, err, json);
});
});
function reqCallback(req, res, err, json) {
if (err) {
endReq(req, res, err);
} else {
endReq(req, res, undefined, json);
}
}
function endReq(req, res, err, json) {
var ret = {};
if (err) {
ret.status = "error";
ret.message = err;
} else {
console.log('success');
ret.status = "ok";
ret.message = "";
}
ret.data = json ? json : {};
res.send(ret);
res.end();
}
app.listen(1000);
console.log('listening on port 1000');