UNPKG

1.07 kBJavaScriptView Raw
1/**
2 * Created by Rodey on 2017/10/31
3 * http web server
4 */
5'use strict';
6
7const http = require('http');
8const util = require('../utils');
9// middlewares
10const bodyParser = require('./middleware/bodyParser');
11const proxy = require('./middleware/proxy');
12const T = require('../tools');
13
14module.exports = next => {
15 return http.createServer((req, res) => {
16 bodyParser(req, res);
17 return useProxy(req, res, next);
18 });
19};
20
21// proxy 代理middeware
22function 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}