-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserverModuleProxy.js
79 lines (63 loc) · 2.42 KB
/
serverModuleProxy.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
//========= proxy endpoints. Used by the connectathon UI to query a server, and by CDS-hooks function =======
const request = require('request');
function setup(app,indb) {
app.get('/proxyfhir/*', function (req, res) {
var fhirQuery = req.originalUrl.substr(11); //strip off /proxyfhir
console.log('proxying: '+ fhirQuery)
var options = {
method: 'GET',
uri: fhirQuery,
encoding: null
};
//options.headers = {accept: 'application/fhir+json'};
options.headers = {accept: 'application/fhir+json, application/json','content-type':'application/json}'};
//options.headers[{'content-type':'application/json}']
request(options, function (error, response, body) {
if (error) {
console.log('error:', error)
var err = error || body;
res.send(err, 500)
} else if (response && response.statusCode !== 200) {
console.log(response.statusCode)
res.send(body, response.statusCode);//,'binary')
} else {
res.send(body);//,'binary')
}
})
});
app.post('/proxyfhir/*', function (req, res) {
var fhirQuery = req.originalUrl.substr(11); //strip off /proxyfhir
let payload= "";
req.on('data', function (data) {
payload += data;
});
req.on('end', function () {
//console.log('payload',payload)
let options = {
method: 'POST',
uri: fhirQuery,
body: payload,
headers: {'content-type': 'application/json'},
encoding: null
};
request(options, function (error, response, body) {
if (error) {
console.log('error:', error)
var err = error || body;
res.send(err, 500)
} else if (response && response.statusCode !== 200) {
console.log('---------------');
console.log(response.statusCode);
console.log(body.toString());
console.log('---------------');
res.send(body, response.statusCode);//,'binary')
} else {
res.send(body);//,'binary')
}
})
})
});
}
module.exports= {
setup : setup
}