forked from chimurai/http-proxy-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (41 loc) · 1.73 KB
/
index.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
var httpProxy = require('http-proxy');
var handlers = require('./lib/handlers');
var contextMatcher = require('./lib/context-matcher');
var pathRewriter = require('./lib/path-rewriter');
var httpProxyMiddleware = function (context, opts) {
var proxyOptions = opts || {};
// Legacy option.proxyHost
// set options.headers.host when option.proxyHost is provided
if (proxyOptions.proxyHost) {
console.log('*************************************');
console.log('[HPM] Deprecated "option.proxyHost"');
console.log(' Use "option.changeOrigin" or "option.headers.host" instead');
console.log(' "option.proxyHost" will be removed in future release.');
console.log('*************************************');
proxyOptions.headers = proxyOptions.headers || {};
proxyOptions.headers.host = proxyOptions.proxyHost;
}
// create proxy
var proxy = httpProxy.createProxyServer(proxyOptions);
// handle option.pathRewrite
if (proxyOptions.pathRewrite) {
var rewriter = pathRewriter.create(proxyOptions.pathRewrite);
proxy.on('proxyReq', function (proxyReq, req, res, options) {
handlers.proxyPathRewrite(proxyReq, rewriter);
});
}
// handle error and close connection properly
proxy.on('error', function (err, req, res) {
handlers.proxyError(err, req, res, proxyOptions);
});
console.log('[HPM] Proxy created:', context, ' -> ', proxyOptions.target);
return middleware;
function middleware (req, res, next) {
if (contextMatcher.match(context, req.url)) {
proxy.web(req, res);
} else {
next();
}
}
};
module.exports = httpProxyMiddleware;