forked from villadora/express-http-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (48 loc) · 2.39 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
'use strict';
// ROADMAP: Major refactoring April 2017
// It would be easier to follow if we extract to simpler functions, and used
// a standard, step-wise set of filters with clearer edges and borders. It
// would be more useful if authors could use Promises for all over-rideable
// steps.
// complete: Break workflow into composable steps without changing them much
// complete: extract workflow methods from main file
// complete: cleanup options interface
// *: change hook names to be different than the workflow steps.
// *: cleanup host is processed twice
// *: cleanup workflow methods so they all present as over-rideable thennables
// *: Update/add tests to unit test workflow steps independently
// *: update docs and release
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 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('handleProxy called on ' + req.path);
var container = new ScopeContainer(req, res, next, host, userOptions);
// Skip proxy if filter is falsey. Loose equality so filters can return
// false, null, undefined, etc.
if (!container.options.filter(req, res)) { return next(); }
buildProxyReq(container)
.then(resolveProxyHost)
.then(decorateProxyReqOpts)
.then(resolveProxyReqPath)
.then(decorateProxyReqBody)
.then(prepareProxyReq)
.then(sendProxyRequest)
.then(copyProxyResHeadersToUserRes)
.then(decorateUserRes)
.then(sendUserRes)
.catch(next);
};
};