-
Notifications
You must be signed in to change notification settings - Fork 2
/
phenobase.api.v1.query.js
55 lines (49 loc) · 1.73 KB
/
phenobase.api.v1.query.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
var express = require('express');
var request = require('request');
var cors = require('cors');
var app = express();
var port = Number(process.env.PORT || 3601);
var apiServerHost = ('http://149.165.170.158:80')
// Listen for requests on all endpoints
//app.use('/', function(req, res, body) {
app.use(cors({origin: '*'}), function(req, res, body) {
// allow connections from JS applications
res.setHeader('Access-Control-Allow-Origin', '*');
//
// short-circuit favicon requests for easier debugging
if (req.url != '/favicon.ico') {
console.log('req.method: ' + req.method);
//console.log('req.url: ' + req.url);
// Request method handling: exit if not GET or POST
if ( ! (req.method == 'GET' || req.method == 'POST') ) {
errMethod = { error: req.method + " request method is not supported. Use GET or POST." };
console.log("ERROR: " + req.method + " request method is not supported.");
res.write(JSON.stringify(errMethod));
res.end();
return;
}
// The incoming requesting string now contains a reference that should be removed
req.url = req.url.replace('/phenobase/api/v1/query/','')
// pass the request to elasticsearch
var url = apiServerHost + req.url;
console.log(url)
req.pipe(request({
uri : url,
auth : {
user : 'username',
pass : 'password'
},
headers: {
'accept-encoding': 'none'
},
rejectUnauthorized : false,
}, function(err, res, body) {
// you could do something here before returning the response
})).pipe(res); // return the elasticsearch results to the user
}
});
// Server Listen
app.listen(port, function () {
console.log('App server is running on http://localhost:' + port);
console.log('apiServerHost: ' + apiServerHost);
});