Skip to content

Commit 2231440

Browse files
author
sky
committed
fix: 静态资源进入 middleware, proxy middleware 插入需要在静态资源和自定义中间件前面
1 parent e7d8980 commit 2231440

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

app.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,19 @@ module.exports = app => {
3434
} else if (config.proxy.force !== true) {
3535
config.proxy.host = `http://127.0.0.1:${port}`;
3636
}
37-
app.middleware.splice(app.middleware.length - 2, 0, convert(proxy(config.proxy)));
37+
// 解决 proxy middleware 插入需要在静态资源和自定义中间件前面
38+
let proxyIndex = -1;
39+
const mwNames = ['static', 'bodyParser', 'overrideMethod', 'session', 'securities', 'notfound', 'siteFile', 'meta'];
40+
while (mwNames.length) {
41+
const name = mwNames.shift();
42+
proxyIndex = app.middleware.findIndex(mw => {
43+
return mw._name === name;
44+
});
45+
if (proxyIndex > -1) {
46+
break;
47+
}
48+
}
49+
app.middleware.splice(proxyIndex, 0, convert(proxy(config.proxy)));
3850
}
3951
});
4052

0 commit comments

Comments
 (0)