我本地測試時, HTTP使用3000端口, HTTPS使用443.
同時監(jiān)聽HTTP和HTTPS
參考上一篇文章Express本地測試HTTPS
轉(zhuǎn)發(fā)所有GET請求
httpApp.get("*", (req, res, next) => { let host = req.headers.host; host = host.replace(//:/d+$/, ''); // Remove port number res.redirect(`https://${host}${req.path}`);});相當(dāng)于自己拼接上https的鏈接然后redirect. 此時瀏覽器會收到302 (MOVED_TEMPORARILY)狀態(tài)碼, 并重定向到HTTPS.
轉(zhuǎn)發(fā)所有請求
httpApp.all("*", (req, res, next) => { let host = req.headers.host; host = host.replace(//:/d+$/, ''); // Remove port number res.redirect(307, `https://${host}${req.path}`);});注意這里面有兩個修改:
只加上第一個修改的話, 重定向的時候不會保留Method, 導(dǎo)致POST請求變成了GET請求. 加上第二個修改就好了.
參考:
How do I redirect all unmatched urls with Express?
Node.js with Express: how to redirect a POST request
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持錯新站長站。
新聞熱點(diǎn)
疑難解答
圖片精選