forked from webpro/dyson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.js
38 lines (32 loc) · 943 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
32
33
34
35
36
37
38
var request = require('request'),
util = require('./util');
var proxyMiddleware = function(req, res) {
var proxyHost = util.options.get('proxyHost'),
proxyPort = util.options.get('proxyPort'),
proxyURI = proxyHost + (proxyPort ? ':' + proxyPort : '') + req.url;
util.log('Proxying', req.url, 'to', proxyURI);
var stream;
if (req._body) {
stream = new require('stream').Readable();
stream._read = function(size) {
this.push(req.rawBody);
this.push(null);
};
} else {
stream = req;
}
stream.pipe(request({
method: req.method,
url: proxyURI,
headers: req.headers
}, function(error) {
if(error) {
util.error('500 INTERNAL SERVER ERROR:', proxyURI);
res.writeHead(500);
res.end();
}
})).pipe(res)
};
module.exports = {
middleware: proxyMiddleware
};