I would like to allow Access-Control-Allow-Origin
(CORS) and ALLOW-FROM
(iframe) for multiple addresses. I've looked around and found this:
Enable Access-Control-Allow-Origin for multiple domains in nodejs
and several others which say to do something like this:
const allowedOrigins = ["https://example1.com", "https://example2.com"];
if (allowedOrigins.includes(req.headers.origin)) {
res.set("Access-Control-Allow-Origin", req.headers.origin);
res.set("X-Frame-Options", `ALLOW-FROM ${req.headers.origin}`);
}
The problem is that req.headers.origin
is always undefined, in development and production server. When I look in the Network tab of the Web Inspector, there is never a request header named origin, only host or sometimes referer.
So I tried the following:
const origin = `${req.protocol}://${req.headers.host}`;
res.set("Access-Control-Allow-Origin", origin);
This is working good, but it can't be used for the X-Frame-Options
header, because host is the address on which the web resides, not the address of the iframe parent. I need ALLOW-FROM https://example1.com
, not ALLOW-FROM https://myapp.com
. Can you help me with it please?