增强 redirect.js,支持path匹配结果拼接重定向地址。
parent
c7f3c23662
commit
0069e1c26b
|
@ -1,7 +1,7 @@
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'redirect',
|
name: 'redirect',
|
||||||
priority: 102,
|
priority: 102,
|
||||||
requestIntercept (context, interceptOpt, req, res, ssl, next) {
|
requestIntercept (context, interceptOpt, req, res, ssl, next, matched) {
|
||||||
const { rOptions, log } = context
|
const { rOptions, log } = context
|
||||||
|
|
||||||
let redirect
|
let redirect
|
||||||
|
@ -15,11 +15,20 @@ module.exports = {
|
||||||
redirect = interceptOpt.redirect(req.url)
|
redirect = interceptOpt.redirect(req.url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 替换内容
|
||||||
|
if (redirect.indexOf('${') >= 0) {
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
// no-template-curly-in-string
|
// no-template-curly-in-string
|
||||||
// eslint-disable-next-line no-template-curly-in-string
|
// eslint-disable-next-line no-template-curly-in-string
|
||||||
redirect = redirect.replace('${host}', rOptions.hostname)
|
redirect = redirect.replace('${host}', rOptions.hostname)
|
||||||
|
|
||||||
|
if (matched) {
|
||||||
|
for (let i = 0; i < matched.length; i++) {
|
||||||
|
redirect = redirect.replace('${m[' + i + ']}', matched[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
res.writeHead(302, {
|
res.writeHead(302, {
|
||||||
Location: redirect,
|
Location: redirect,
|
||||||
'DS-Interceptor': 'redirect'
|
'DS-Interceptor': 'redirect'
|
||||||
|
|
Loading…
Reference in New Issue