forked from brianGammon/fhir-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.js
31 lines (27 loc) · 957 Bytes
/
proxy.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
var Request = require('request');
var Router = module.exports = require('express').Router();
Router.use('/:protocol/:host', function(req, res, next) {
req.proxy = {path: '', protocol: req.params.protocol, host: req.params.host};
next();
})
Router.use('/:protocol/:host/:path*', function(req, res, next) {
var path = req.params.path;
var prefix = '/proxy/' + req.params.protocol + '/' + req.params.host + '/';
req.proxy.path = req.originalUrl.substring(prefix.length);
next();
});
Router.all('*', function(req, res) {
var url = req.proxy.protocol + '://' + req.proxy.host + '/' + req.proxy.path;
var handleErr = function() {
res.status(500).json({error: "Error connecting to " + url})
}
try {
req.pipe(Request({
url:url,
qs: req.query,
rejectUnauthorized: require('./options.js').reject_unauthorized,
}).on('error', handleErr)).pipe(res).on('error', handleErr);
} catch (e) {
handleErr(e);
}
});