always set req.originalHostPrefix

This commit is contained in:
mikiher 2024-12-08 09:23:39 +02:00
parent 9b8e059efe
commit a8ab8badd5

View File

@ -251,18 +251,17 @@ class Server {
const router = express.Router() const router = express.Router()
// if RouterBasePath is set, modify all requests to include the base path // if RouterBasePath is set, modify all requests to include the base path
if (global.RouterBasePath) {
app.use((req, res, next) => { app.use((req, res, next) => {
const urlStartsWithRouterBasePath = req.url.startsWith(global.RouterBasePath)
const host = req.get('host') const host = req.get('host')
const protocol = req.secure || req.get('x-forwarded-proto') === 'https' ? 'https' : 'http' const protocol = req.secure || req.get('x-forwarded-proto') === 'https' ? 'https' : 'http'
const prefix = req.url.startsWith(global.RouterBasePath) ? global.RouterBasePath : '' const prefix = urlStartsWithRouterBasePath ? global.RouterBasePath : ''
req.originalHostPrefix = `${protocol}://${host}${prefix}` req.originalHostPrefix = `${protocol}://${host}${prefix}`
if (!req.url.startsWith(global.RouterBasePath)) { if (!urlStartsWithRouterBasePath) {
req.url = `${global.RouterBasePath}${req.url}` req.url = `${global.RouterBasePath}${req.url}`
} }
next() next()
}) })
}
app.use(global.RouterBasePath, router) app.use(global.RouterBasePath, router)
app.disable('x-powered-by') app.disable('x-powered-by')