【问题标题】:Mixed content error for http express routehttp express 路由的混合内容错误
【发布时间】:2018-05-12 05:38:15
【问题描述】:

我在我的机器上运行一个本地节点 js 服务器,我为它提供了一个 ngrok.io 域。

如果我在网络浏览器中使用localhost:3000http://1234.ngrok.io 在本地运行它,一切正常。如果我用httpS://1234.ngrok.io 调用它,我会收到错误

Mixed Content: The page at 'https://1234.ngrok.io/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://localhost:3000/calculation'. This request has been blocked; the content must be served over HTTPS.

因为路由不是http造成的。我该如何解决这个问题?

这是我 node.js 上的 app.js

const express = require('express');
const app = express();
const https = require('https');
const http = require('http');
const fs = require('fs');

app.use('/', express.static(__dirname + '/../Client'));

var options = {
  key: fs.readFileSync('./https_certificate/client-key.pem'),
  cert: fs.readFileSync('./https_certificate/client-cert.pem')
};

app.listen(server_connection.port, () => {
    console.log('Server started on port ' + server_connection.port);
    //do stuff
});

//load the index.html from the server
app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname + '/../Client/index.html'));
});

这是导致错误的路由的另一个文件:

const express = require('express');
const router = express();
router.post('/calculation', function(req, res) {
 //do stuff
}

我已经尝试为 https 配置服务器,您如何查看第一个文件中的 options 对象,但这在 502 Bad Gateway 中得到解决。

我也想只使用 https。如果有人使用http,是否可以重定向到https?

【问题讨论】:

    标签: node.js http express https routes


    【解决方案1】:

    您似乎正在使用 http 向服务器发送 ajax 请求,请确保您的所有 ajax 调用都已结束 https 以摆脱此错误。

    【讨论】:

      猜你喜欢
      • 2018-04-05
      • 2016-04-16
      • 2015-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 2018-06-28
      相关资源
      最近更新 更多