【发布时间】:2018-11-21 07:32:40
【问题描述】:
除了 postgresql 数据库之外,我还有一个使用 node.js 和 socket.io 库的 Web 应用程序。它托管在heroku上。对于 socket.io 库,如果我使用输入为“http://X”的站点地址运行代码,但在尝试“https://X”时不会收到错误消息 为了解决这个错误,如果他们访问 http 版本,我会强制将用户重定向到 https 站点。这是我的代码。
<script>
if (document.URL.indexOf("https") == -1 && document.URL.indexOf("localhost") == -1)
{
window.location.href = document.URL.replace("http", "https");
}
</script>
有什么理由说明这是一个坏主意吗?有更好的解决方案吗?任何关于 http 和 https 之间区别的 cmets 将不胜感激。这是我在 http 上运行网站时遇到的错误:
WebSocket connection to 'ws://theland.herokuapp.com/socket.io/?EIO=3&transport=websocket&sid=<not sure if I should share this part of the url...>' failed: Error during WebSocket handshake`: 'Sec-WebSocket-Accept' header is missing
澄清一下,该网站现在可以很好地使用我的解决方案,但我担心我的解决方案是否是“良好做法”。
感谢您的帮助!
【问题讨论】:
-
我写了一个答案,有帮助吗?
标签: html node.js http https socket.io