Express中间件针对编码的URL陷入无限循环,
我的网址中有法文字母,因此每次尝试打出类似/équipement
快递服务器遇到一个无限循环,试图处理URL(/%C3%A9quipement/
),但出现ERR_TOO_MANY_REDIRECTS
错误。
这不会在我的本地环境中发生,而是在Azure托管的开发环境中发生,并且正在运行IIS Web服务器。
已配置3个中间件,
server.js
app.use(middlewares.ipcheck)
app.use('/:lang*', middlewares.redirection)
app.use(microcache.cacheSeconds(600, middlewares.microcache))
基于Azure日志,代码会命中microcache
中间件,然后返回到ipcheck
中间件。
ipcheck.js
if (req.query.skipGeo || req.cookies.geoCheck || isBot(req)) {
console.log(`Cookie Present \n`)
let hour = 3600000
res.cookie('geoCheck', 'passed', { maxAge: 365 * 24 * hour })
next()
} else {
console.log(`Checking against mmdb \n`)
const isValidIP = await user.hasValidIp(req.headers['x-forwarded-for'] || req.connection.remoteAddress)
process.env.isValidIP = isValidIP
if (!isValidIP) {
console.log(`Geo Block \n`)
res.status(307).sendFile(path.resolve('public/geo_block.html'))
} else {
console.log(`Success \n`)
next()
}
}
microcache.js
return config.useMicroCache.get() && `${req.hostname}${req.originalUrl}` // useMicroCache is a bool val set based on env.
redirection.js
const params = req.params
let lang = params.lang
const site = utils.user.currentSite(req)
if (!req.originalUrl.includes('api')) {
if (!validLanguage(site, lang)) {
lang = req.cookies.lang && validLanguage(site, req.cookies.lang) ? req.cookies.lang : site.languages.default
config.useMicroCache.set(false)
res.redirect(301, `/${lang}${req.originalUrl}`)
return
} else if (lang !== req.cookies.lang) {
config.useMicroCache.set(false)
} else {
config.useMicroCache.set(true)
}
if (!isLowerCase(req.path)) {
let parsedUrl = url.parse(req.originalUrl)
parsedUrl.pathname = parsedUrl.pathname.toLowerCase()
res.redirect(301, url.format(parsedUrl))
return
}
}
next()