第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

HTTP 重定向循環(huán) Node/Express

HTTP 重定向循環(huán) Node/Express

FFIVE 2023-11-02 21:14:58
我有以下代碼用于嘗試將任何http流量重定向到等效的https// Add some redirect logic to ensure that https is always used in production, staging, development environmentapp.use((req, res, next) => {  // 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()  if(!req.secure) {    return res.redirect(301, `https://${req.headers.host}${req.originalUrl}`)  }  next()})/** * Bootstrap routes */require('./routes')(app)然而,當(dāng)我在瀏覽器中測(cè)試這個(gè)并輸入類似http://example.com我在瀏覽器中收到重定向循環(huán)警告和我的 papertrail 日志中的內(nèi)容時(shí),我可以看到 console.log 一次又一次地觸發(fā)。我在這里缺少一些簡(jiǎn)單的東西嗎?任何幫助表示贊賞。
查看完整描述

2 回答

?
慕運(yùn)維8079593

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊

添加app.enable('trust proxy')修復(fù)了問題并允許上面的代碼工作。同樣,我正在使用 Heroku。


查看完整回答
反對(duì) 回復(fù) 2023-11-02
?
墨色風(fēng)雨

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)論中提到的那樣


查看完整回答
反對(duì) 回復(fù) 2023-11-02
  • 2 回答
  • 0 關(guān)注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)