I'm getting the error below in the console of my browser:
Content Security Policy: The page’s settings blocked the loading of a resource at http://localhost:3000/favicon.ico
(“default-src”).
I searched online and saw that this should be fixed with the snippet of code below:
<meta http-equiv="Content-Security-Policy" content="default-src *;
img-src * 'self' data: https: http:;
script-src 'self' 'unsafe-inline' 'unsafe-eval' *;
style-src 'self' 'unsafe-inline' *">
I added this to my front-end app.component.html
file (the parent template for all my front-end views), but this didn't work as expected.
I've also tried multiple permutations thereupon to no avail.
My front-end is at localhost:4200
and back-end at localhost:3000
.
Below is the snippet of code from my back-end server (middleware):
app.use(cors());
app.options('*',cors());
var allowCrossDomain = function(req,res,next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
}
app.use(allowCrossDomain);
I also have now added the following middleware to my backend (Express) server:
const csp = require('express-csp-header');
app.use(csp({
policies: {
'default-src': [csp.SELF, 'http://localhost:3000/', 'http://localhost:4200/' ],
'script-src': [csp.SELF, csp.INLINE],
'style-src': [csp.SELF],
'img-src': ['data:', 'favico.ico'],
'worker-src': [csp.NONE],
'block-all-mixed-content': true
}
}));
. . . but still hasn't fixed the problem.
Here is a screenshot:
What am I doing wrong and how can I fix it?
See Question&Answers more detail:
os