我正在尝试将我的 Angular 应用程序部署到 Heroku,但遇到了很多问题。我收到错误“拒绝加载图像 ' https://thawing-fortress-79986.herokuapp.com/favicon.ico ',因为它违反了以下内容安全策略指令:“default-src 'none'”。请注意,未显式设置 'img-src',因此 ' default-src' 用作后备。”
我尝试添加 http://* https://* '不安全内联'; script-src'self'http://* https://* 'unsafe-inline' 'unsafe-eval'"> 到我的index.html,我尝试运行 ng build 但没有解决任何问题
不确定这个错误来 self 的代码中的哪个位置,但这是我服务器的一部分
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 文件,但它在页面上给我一个“内部服务器错误”和一个“”拒绝加载图像' https://thawing-fortress-79986.herokuapp.com/favicon.ico',因为它违反了以下内容安全策略指令:“default-src 'none'”。请注意,“img-src”未明确设置,因此“default-src”用作后备。“”在控制台注销中。
任何帮助将不胜感激
最佳答案
可能路径没有正确给出。 转到 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'));
关于node.js - 将 Angular 应用程序部署到 Heroku 时出现 "violates the following Content Security Policy directive: "default-src 'none' ". 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57454623/