我有 http 服务器,但我不知道它为什么不运行;
index.js 文件:
const express = require("express");
const app = (global.app = express());
const helmet = require("helmet");
const server = require("http").createServer(app);
const io = (global.io = require("socket.io")(server));
const config = require("./config.js");
//Middleware
app.use(helmet());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
require("./socket")(io);
require("./router")(app);
server.listen(config.PORT, () => console.log(`Listen on port ${config.PORT}...`));
路由器文件:const express = require("express");
const path = require("path");
module.exports = (app) => {
app.use(express.static(path.join(__dirname, "/../public/main")));
app.use("/board/:id", express.static(path.join(__dirname, "/../public/paint")));
app.get("*", (req, res) => {
res.status(404).sendFile(path.join(__dirname, "/../", "/public/404/404.html"));
});
};
Chrome 显示错误:GET https://10.0.1.55:5000/board/jsdbf/style.css net::ERR_SSL_PROTOCOL_ERROR
GET https://10.0.1.55:5000/socket.io/socket.io.js net::ERR_SSL_PROTOCOL_ERROR
适用于所有 js、css 和 imgs。 index.html 正确加载。
最佳答案
我不是内容安全策略方面的 100% 专家,但我注意到了一些事情。
如Benjamin Lemoine指出,net::ERR_SSL_PROTOCOL_ERROR
使用 Helm 时出现问题 (app.use(helmet())
)。我不认为解决方案是根本不使用 Helm ,因为它为应用程序提供了安全性。
对所有 Helm 进行暴力破解后middlewares (contentSecurityPolicy、dnsPrefetchControl、expectCt、frameguard 等),我发现 contentSecurityPolicy
正在创建错误。 WDN文档说 升级不安全请求 指令指示用户代理将站点的所有不安全 URL(通过 HTTP 提供的 URL)视为已替换为安全 URL(通过 HTTPS 提供的 URL)。
所以为了解决问题并同时使用 Helm ,我改变了这个:
app.use( helmet() );
对此:app.use( helmet() );
app.use(helmet.contentSecurityPolicy({
directives: {
...defaultDirectives,
},
}));
这样我仍在使用 Helm ,但 CSP 指令并未强制 HTPP 使用 HTTPS。请记住 升级不安全请求 应在生产环境中设置指令。
关于node.js - http服务器中的NodeJS err-ssl-protocol-error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64534727/