首页 > 解决方案 > 获取“违反以下内容安全策略指令:“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.

任何帮助将不胜感激

标签: node.jsangularheroku

解决方案


可能路径没有正确给出。转到 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'));

推荐阅读