-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (82 loc) · 2.5 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* @Author: zhangrongmou
* @Date: 2018-08-02 22:04:19
* @Last Modified by: zhangrongmou
* @Last Modified time: 2018-08-21 20:23:21
*/
const util = require('util');
const querystring = require('querystring');
const request = require('request');
const _ = require('lodash');
const utils = require('./utils/utils.js');
const colors = require('colors');
colors.setTheme({
input: 'grey',
verbose: 'cyan',
prompt: 'red',
info: 'green',
data: 'blue'
});
module.exports = (options) => {
options = _.defaults(options || {}, {
body_parse: true,
proxy_timeout: 3000,
proxy_methods: ['GET', 'POST', 'PUT', 'DELETE'],
host: false,
rules: [],
gzip: true
});
return async (ctx, next) => {
if (utils.shouldSkipNext(ctx, options)) {
return await next();
};
let opts = {};
if (utils.shouldParseBody(ctx, options)) {
ctx.request.body = await utils.execParseBody(ctx, false);
}
if (util.isError(ctx.request.body)) {
ctx.status = 500;
return;
}
opts = utils.configRequestOptions(ctx, options);
if (!opts.host) {
delete opts.headers.host;
}
await requestWrap(opts).then(res => {
let result = res.body;
// 如果是json数据, 则解析为对象
if (/json/i.test(res.headers['content-type'])) {
ctx.body = JSON.parse(result);
}
['content-type', 'set-cookie'].forEach(header => {
res.headers[header] && ctx.set(header, res.headers[header]);
});
}).catch(err => {
ctx.body = err;
});
};
};
function requestWrap(opt) {
return new Promise((resolve, reject) => {
let fullUrl = opt.url;
if (querystring.stringify(opt.qs)) {
fullUrl += '?' + querystring.stringify(opt.qs);
}
request(opt, (error, response) => {
let status = response && response.statusCode;
// success
if (!error && +status === 200) {
console.log(`[proxy] ${fullUrl}`.green);
console.log(response.body);
resolve(response);
} else {
console.log(`[proxy] ${fullUrl}`.red);
console.log(response.body);
reject({
result: status,
msg: '服务请求出错!'
});
}
})
});
}