himesh
himesh

Reputation: 77

Getting host name of client url in node.js

I am sending data from JavaScript client to node.js server using XMLHttpRequest ajax. Now on server side I need to know the host of URL of the page the JavaScript calling XMLHttpRequest is embedded in.

Server side:

req.on('data', function(data) {
  var d = JSON.parse(data);
  if (d.Type == "abc") {
    var host = req.headers.Host;
    var reply = {
      "hostname": host
    };
    console.log("hostname :" + host);
    response.end(JSON.Stringify(reply));
  }
});

Expected result on console: hostname: hostname

Actual result on console: hostname: undefined

Upvotes: 1

Views: 3189

Answers (2)

James Serengia
James Serengia

Reputation: 159

Try this

const clientUrl = req.header('Referer');
console.log(clientUrl);

Upvotes: 0

Quentin
Quentin

Reputation: 943552

Browsers include a Referer HTTP Request header as normal for XHR requests. You can examine that.

Upvotes: 2

Related Questions