2 回答
TA贡献1853条经验 获得超6个赞
尝试添加:到 中的协议名称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:这是因为 url 解析器返回协议为https:. 请参阅 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像评论中提到的那样
添加回答
举报