I have a simple index.html file. This html is loading from localhost:3000
<body>
<h1>Some app</h1>
<script src="http://localhost:3005/script.js"></script>
</body>
The script.js is set to load from another origin: localhost:3005.
From my understanding, The browser should intercept the request and add the origin header to it - and make a preflight request to localhost:3005 server to check if localhost:3000 is allowed.
But i'm getting no origin. The browser doesn't seem to add the origin header.
This is the server responsible for serving the script.js file (localhost:3005). I'm using the cors module.
const app = express()
const whitelist = ["http://localhost:3001"]
const corsOptions = {
origin: function(origin, callback) {
console.log("origin is: ", origin); //undefined -- why is undefined??
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
}
else {
callback(new Error('Not allowed by CORS'))
}
}
}
app.use(cors(corsOptions), express.static(__dirname))
app.listen(3005, () => {
console.log("server listening on port 3005")
})
My actual goal is to white-list only localhost:3000 for asking for my script.js
And i put localhost:3001 just to test if i'm actually getting a cors error - as it should be.
But i'm not even getting the origin - what is causing this? I'm getting the same behavior in both Firefox and Chrome. Did i misunderstood this hole process? Thanks :)
EDIT: Maybe seeing the request helps

srcURLs. Those requests are made with the fetch mode set to 'no-cors', and the Origin request header is not sent for 'no-cors' requests. Basically, browsers send the Origin header only for requests made using XHR or the Fetch API (or from JavaScript-library Ajax methods build on XHR or the Fetch API) or if the request HTTP method isn’t GET, or if the request is initiated from an HTML element with the 'crossorigin' attribute. fetch.spec.whatwg.org has the details