forked from neontribe/r4d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
72 lines (58 loc) · 2.12 KB
/
web.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
var http = require("http")
,fs = require('fs')
,querystring = require('querystring')
,connect = require("connect")
,ejs = require("ejs")
,port = process.env.PORT || 3000
,kasabi_api_key = "368389391f29f9442406b400a42e1dfd6eaacb22"
,kasabi_host = "api.kasabi.com"
,kasabi_path = "/dataset/r4d-aid-data/apis/sparql"
,sparql;
/**
* Precompile our sparql request template and store it
*/
fs.readFile('sparql.ejs', 'utf8', function(err, data) {
if(!err) {
sparql = data;
}
});
connect(
connect.logger(),
connect.static(__dirname + "/public"),
// create a router to handle application paths
connect.router(function(app) {
app.get("/project/:prid", function(req, res) {
var query = ejs.render(sparql, {
prid : req.params.prid,
kasabi_api_key : kasabi_api_key
}),
opts = {
host : kasabi_host,
path : kasabi_path + '?' + query,
port : "80"
},
req_params = querystring.parse(req.url.split("?")[1]);
http.get(opts, function(response){
var results = '';
response.on('data', function(chunk) {
results += chunk;
});
response.on('end', function(){
//accept jsonp requests
if (req_params.callback) {
// if we have a callback function name, do JSONP
results = req_params.callback + "(" + results + ");";
}
// write the results to the output
res.writeHead(200, {
// change MIME type to JSON
"Content-Type": (req_params.callback) ? "application/javascript" :"application/json",
"Content-Length": results.length
});
res.end(results)
});
});
});
})
).listen(port);
console.log('r4d server running on port ' + port);