It looks like req.url contains a forward slash and the query string from the URL, but not the hostname.
From: Justin Taylor
Sent: Monday, January 15, 2018 12:40 PM
To: OpenSource-Midrange. com (opensource@xxxxxxxxxxxx) <opensource@xxxxxxxxxxxx>
Subject: Node Access-Control-Allow-Origin
I'm trying to call a Node webservice from an existing app running on Apache. It's giving me problems because the app is running on a different port than Node. I did some searching, and I was able to get it to work by adding a new property to my header response:
'Content-Type' : 'application/json'
, 'Access-Control-Allow-Origin' : 'http://myHostName:80
' // Added this line
The trouble is that I have multiple production and test Apache servers, and I don't want to hardcode them all. They all have the same hostname, but I can't find a way to do a wildcard for the port#. What I got from Google, was to manually match against the calling host. If it matches, simply parrot it back for the Access-Control-Allow-Origin. The example I found returns null for me for the calling host. Here's what I found:
console.log(url.parse(req.url, true).host); //should return 'myHostName:80', but I get null