I'm trying to deploy an angular application to heroku, and to do so I had to create a sever.js file so that the deployment can be done using node. The problem is that any request from my app to my api app (deployed using container registry in heroku too) is being blocked by cors.
server.js :
function requireHTTPS(req, res, next) {
// The 'x-forwarded-proto' check is for Heroku
next();
}
const allowedOrigins = ['my-api'];
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors({
credentials: true,
origin: (origin, callback) => {
if (!origin || allowedOrigins.includes(origin)) {
console.log(origin);
callback(null, true) ;
} else {
callback(new Error(`Origin: ${origin} is now allowed`))
}
}
}));
app.use(requireHTTPS);
app.use(express.static('./dist/HygeneSpa'));
app.get('/*', function(req, res) {
res.sendFile('index.html', {root: 'dist/HygeneSpa/'}
);
});
app.listen(process.env.PORT || 8080);
The error :
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at api-app. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
One other thing that I've noticed is that the OPTIONS request does not contain a cors header, but the POST request does.