forked from villadora/express-http-proxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
61 lines (53 loc) · 2.54 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
55
56
57
58
59
60
'use strict';
// * Breaks proxying into a series of discrete steps, many of which can be swapped out by authors.
// * Uses Promises to support async.
// * Uses a quasi-Global called Container to tidy up the argument passing between the major work-flow steps.
var ScopeContainer = require('./lib/scopeContainer');
var assert = require('assert');
var debug = require('debug')('express-http-proxy');
var buildProxyReq = require('./app/steps/buildProxyReq');
var copyProxyResHeadersToUserRes = require('./app/steps/copyProxyResHeadersToUserRes');
var decorateProxyReqBody = require('./app/steps/decorateProxyReqBody');
var decorateProxyReqOpts = require('./app/steps/decorateProxyReqOpts');
var decorateUserRes = require('./app/steps/decorateUserRes');
var decorateUserResHeaders = require('./app/steps/decorateUserResHeaders');
var filterUserRequest = require('./app/steps/filterUserRequest');
var handleProxyErrors = require('./app/steps/handleProxyErrors');
var maybeSkipToNextHandler = require('./app/steps/maybeSkipToNextHandler');
var prepareProxyReq = require('./app/steps/prepareProxyReq');
var resolveProxyHost = require('./app/steps/resolveProxyHost');
var resolveProxyReqPath = require('./app/steps/resolveProxyReqPath');
var sendProxyRequest = require('./app/steps/sendProxyRequest');
var sendUserRes = require('./app/steps/sendUserRes');
module.exports = function proxy(host, userOptions) {
assert(host, 'Host should not be empty');
return function handleProxy(req, res, next) {
debug('[start proxy] ' + req.path);
var container = new ScopeContainer(req, res, next, host, userOptions);
filterUserRequest(container)
.then(buildProxyReq)
.then(resolveProxyHost)
.then(decorateProxyReqOpts)
.then(resolveProxyReqPath)
.then(decorateProxyReqBody)
.then(prepareProxyReq)
.then(sendProxyRequest)
.then(maybeSkipToNextHandler)
.then(copyProxyResHeadersToUserRes)
.then(decorateUserResHeaders)
.then(decorateUserRes)
.then(sendUserRes)
.catch(function (err) {
// I sometimes reject without an error to shortcircuit the remaining
// steps and return control to the host application.
if (err) {
var resolver = (container.options.proxyErrorHandler) ?
container.options.proxyErrorHandler :
handleProxyErrors;
resolver(err, res, next);
} else {
next();
}
});
};
};