nodejs express强制留在http

nodejs express force to stay on http

在 NodeJs 应用程序上工作,我想欺骗浏览器以保留 http,最新版本的浏览器会继续将其重定向到 https。

我知道不建议这样做,但它仅适用于 PoC,因此不安全的通信没有大问题。

var port = 80;

server.listen(port, function () {
    console.log('webapp: listening at port %d', port);
});

    // Routing
    const currentDirectory = path.join(__dirname, '../', 'webapp');
    app
        .get('/', applyTemplate('index.html'))
        .get('/index.html', applyTemplate('index.html'))
        .use(express.static(currentDirectory))

我可以欺骗浏览器吗?在不创建 ssl 证书的情况下处理 https 连接并将其重定向到 http。

您需要先用openssl创建一个自签名证书,然后再使用。 如果没有,现代浏览器将尝试通过阻止某些操作来保护您。这些检查非常重要,您应该知道它们的存在,以便编写正确且有效的代码。如果您开始更改浏览器行为,您可以轻松地在 HTTPS 和 HTTP 之间发送密码或敏感数据。我可以问你为什么要跳过这些检查吗? 大多数时候,一个做得好的测试套件足以以适当的方式处理这些情况,并获得浏览器的正确响应。

我附上了一个示例,您可以在 => https://timonweb.com/posts/running-expressjs-server-over-https/

找到
openssl req -nodes -new -x509 -keyout server.key -out server.cert
var express = require('express')
var fs = require('fs')
var https = require('https')
var app = express()

app.get('/', function (req, res) {
  res.send('hello world')
})

https.createServer({
  key: fs.readFileSync('server.key'),
  cert: fs.readFileSync('server.cert')
}, app)
.listen(3000, function () {
  console.log('Example app listening on port 3000! Go to https://localhost:3000/')
})