forked from alibaba/ice
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdevMiddleware.js
More file actions
34 lines (30 loc) · 1.08 KB
/
devMiddleware.js
File metadata and controls
34 lines (30 loc) · 1.08 KB
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
const httpProxyMiddleware = require('http-proxy-middleware');
const getProxyConfig = require('./config/getProxyConfig');
// 附加中间件流程
module.exports = (app) => {
const proxyConfig = getProxyConfig();
if (proxyConfig) {
const proxyRules = Object.entries(proxyConfig);
proxyRules.forEach(([match, opts]) => {
if (opts.enable) {
const proxyOptions = {
context: match,
target: opts.target,
changeOrigin: true,
logLevel: 'warn',
onProxyRes: function onProxyReq(proxyRes, req, res) {
proxyRes.headers['x-proxy-by'] = 'ice-proxy';
proxyRes.headers['x-proxy-match'] = match;
proxyRes.headers['x-proxy-target'] = opts.target;
if (opts.target && opts.target.endsWith('/')) {
opts.target = opts.target.replace(/\/$/, '');
}
proxyRes.headers['x-proxy-target-path'] = opts.target + req.url;
},
};
const exampleProxy = httpProxyMiddleware(match, proxyOptions);
app.use(exampleProxy);
}
});
}
};