2 回答

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊
添加app.enable('trust proxy')
修復(fù)了問題并允許上面的代碼工作。同樣,我正在使用 Heroku。

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超6個(gè)贊
嘗試添加:到 中的協(xié)議名稱req.protocol === 'https:'。
// Add some redirect logic to ensure that https is always used in production, staging, development environment
app.use((req, res, next) => {
console.log('here')
// if NODE_ENV is 'local' don't redirect to https, only do so for our deployed server environments
if(!['development', 'staging', 'production'].includes(process.env.NODE_ENV)) return next()
// request was via https, so do no special handling
if(req.protocol === 'https:') return next(); // <---- here, add : after https
res.redirect('https://' + req.headers.host + req.url)
})
/**
* Bootstrap routes
*/
require('./routes')(app)
UPD:這是因?yàn)?url 解析器返回協(xié)議為https:. 請(qǐng)參閱 Nodejs repl 模式的示例
$ node
> url.parse('https://ya.ru')
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'ya.ru',
port: null,
hostname: 'ya.ru',
hash: null,
search: null,
query: null,
pathname: '/',
path: '/',
href: 'https://ya.ru/'
}
https:,而不是https像評(píng)論中提到的那樣
添加回答
舉報(bào)