node.js - http服务器中的NodeJS err-ssl-protocol-error

标签 node.js express ssl

我有 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/

相关文章:

node.js - nodejs 挂起 express 静态文件

javascript -  '{ title: string; description: string; }' 类型的参数不可分配给  '{ id: {}; title: {}; description: {}; }' 类型的参数

asp.net - 为 MVC3 应用程序中的某些操作启用和要求 SSL

JAVA - Tomcat 5 应用程序使用 HTTPS 连接到 Web 服务,而 Tomcat 7 应用程序不

javascript - Node.js 请求数据损坏

javascript - 编写异步函数时的具体困惑。有人可以验证吗?

http - NodeJS 请求未返回

node.js - 设置 Jade 布局的 CSS 路径

ssl - keytool -list -v 和 keytool -list 返回不同的输出

angularjs - 带有 jsonp 负载的 Express.js 响应状态代码