1 |
|
2 |
|
3 |
|
4 |
|
5 | 'use strict';
|
6 |
|
7 | const http = require('http');
|
8 | const util = require('../utils');
|
9 |
|
10 | const bodyParser = require('./middleware/bodyParser');
|
11 | const proxy = require('./middleware/proxy');
|
12 | const T = require('../tools');
|
13 |
|
14 | module.exports = next => {
|
15 | return http.createServer((req, res) => {
|
16 | bodyParser(req, res);
|
17 | return useProxy(req, res, next);
|
18 | });
|
19 | };
|
20 |
|
21 |
|
22 | function useProxy(req, res, next) {
|
23 | const gupackConfig = T.getConfig() || {};
|
24 | const proxyConfig = gupackConfig.proxy;
|
25 | if (proxyConfig) {
|
26 | if (util.isString(proxyConfig)) {
|
27 | proxyConfig = { context: '/', target: proxyConfig };
|
28 | }
|
29 | if (proxy.isMatchedContext(proxyConfig.context, req)) {
|
30 | return proxy
|
31 | .startProxy(req, res)
|
32 | .then(() => {
|
33 | next(req, res);
|
34 | })
|
35 | .catch(err => console.log(err));
|
36 | }
|
37 | }
|
38 | next(req, res);
|
39 | }
|