forked from kevinclement/SimpleArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrunt.connect.routes.js
72 lines (62 loc) · 2.59 KB
/
grunt.connect.routes.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
// These are special routes I only use during development mode to aid in /admin sections
'use strict';
var bodyParser = require('body-parser');
var request = require('request');
var cachedWowheadRequest;
exports.getMiddleware = function() {
return [
bodyParser.json({limit: '5mb'}), // will parse json data out of the body
function(req, res, next) {
// NOTE: special contract with me and admin section of site that allows
// me to post json to /save/<file>/, and I'll write it to <file>.json on disk
var saveUrl = req.url.indexOf('/save/') === 0;
if (!saveUrl) return next();
var fileToSaveTo = 'app/data/' + req.url.replace('/save/', '') + '.json';
require('fs').writeFile(
fileToSaveTo,
JSON.stringify(req.body, null, ' '));
res.end('Uploaded to ' + fileToSaveTo);
},
function(req, res, next) {
// This URL allows me to query wowhead for criteria they use
// I can't query it from browser because of CORS, so I send a request
// to localhost and then I can query from node to the wowhead api
var criteriaUrl = req.url.indexOf('/criteria/') === 0;
if (!criteriaUrl) return next();
// No need to query from wowhead for every refresh in admin session, restart dev env for a refresh
if (cachedWowheadRequest) {
res.setHeader('Content-Type', 'application/json');
res.end(cachedWowheadRequest);
} else {
request('http://www.wowhead.com/data=achievements', function (error, response, body) {
if (!error && response.statusCode == 200) {
// criteria mapping
var myRegexp = /g_achievement_criteria = (.*);/g;
var match = myRegexp.exec(body);
// You should end up with a json like
// {
// "achievementId" :
// {
// "wowheadId":
// [
// [
// count,
// criteriaId
// ]
// ]
// }
// }
cachedWowheadRequest = match[1];
res.setHeader('Content-Type', 'application/json');
res.end(match[1]);
}
else {
console.log("Trouble getting javascript from wowhead. code: " + response.statusCode);
console.log(" " + response.body);
res.end(response.statusCode);
}
});
}
}
];
}