node.js - 获取“违反以下内容安全策略指令:“default-src 'none'”。将 Angular 应用程序部署到 heroku 时出错
问题描述
我正在尝试将我的 Angular 应用程序部署到 Heroku,但我遇到了很多问题。我收到错误“拒绝加载图像' https://thawing-fortress-79986.herokuapp.com/favicon.ico ',因为它违反了以下内容安全策略指令:“default-src 'none'”。请注意,'img-src' 没有明确设置,所以 'default-src' 被用作后备。”
我试过添加 http://* https://* 'unsafe-inline'; script-src 'self' http://* https://* 'unsafe-inline' 'unsafe-eval'"> 到我的 index.html 并且我尝试运行 ng build 但这并没有解决任何问题
不知道这个错误来自我的代码的哪里,但这是我的服务器的一部分
app.use(express.static(__dirname + '/dist'));
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname + '/dist/index.html'));
});
这是我的 package.json 的一部分
"name": "*******",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "node server.js",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
我试图部署到 Angular,我希望它启动我的服务器并加载我的静态 Angular 文件,但它在页面上给了我一个“内部服务器错误”和一个""Refused to load the image 'https://thawing-fortress-79986.herokuapp.com/favicon.ico' because it violates the following Content Security Policy directive: "default-src 'none'". Note that 'img-src' was not explicitly set, so 'default-src' is used as a fallback."" in the console logout.
任何帮助将不胜感激
解决方案
可能路径没有正确给出。转到 dist 文件夹并检查 index.html 的正确路径
例子 :
'/dist/**<your app name>**' && '/dist/**<your app name>**/index.html'
并在以下两行中更改路径
app.use(express.static(__dirname + '/dist'));
res.sendFile(path.join(__dirname + '/dist/index.html'));
推荐阅读
- python - 计算多个文件中 3 克术语的出现次数
- c# - EF Core 中是否有可能需要单向导航属性?
- python - 了解 Conv1D、密集层(一维输入)的 keras input_shape
- c++ - 如何使用结构化绑定来复制具有类型为 T& 的元素的类元组对象?
- laravel - Laravel - 更改密码问题
- java - Vaadin 14 如何将 vaadin servlet 移动到不同的 URL?
- c# - 如何在 replaceImage Google 文档请求中使用 Google Drive 存储的 jpeg 图像?
- node.js - 在 nodejs 中设计一个简单的缓存管理器来管理磁盘上的文件
- python - QWebEngineView 历史 canGoForward/canGoBack 仅在三项后有效
- exception - 更新:WiFiClientSecure (Telegram) 和 WiFiClient (MQTT) 的内存泄漏