Reputation: 3498
I have two node.js applications. One backend api.domain and one frontend domain. I use express and passport. When trying to login in ... What I need in the api.domain app is the domain/url of the domain making the request.
But all my attempts failed:
In api.domain app:
const curIP = req.ip;
const curHostname = req.hostname;
console.log('IP: '+curIP);
console.log('Hostname: '+curHostname);
const curGetHost = req.get('host');
console.log('curGetHost: ' +curGetHost);
const curHost = req.host;
console.log(curHost);
var headerUserAgent = req.header('User-Agent');
console.log(headerUserAgent);
var origin = req.get('origin');
console.log(origin);
var headersOrigin = req.headers.origin;
console.log(headersOrigin);
var headersXForwarded = req.headers["x-forwarded-for"];
console.log(headersXForwarded);
var headersHost = req.headers["host"];
console.log(headersHost);
var remoteAddress = req.connection.remoteAddress;
console.log(remoteAddress);
It always outputs the domain of the api.domain app, not the domain app. How can I get the domain/url of the client trying to access my api.domain app?
Edit: On client side that's my request:
request.post({
headers: {
"Content-Type": "application/json",
"Referer": DOMAIN
},
url: URL_TARGET+"/v1/login",
body: JSON.stringify({
username: username,
password: password
})
}, ...
Upvotes: 1
Views: 2787
Reputation: 1092
You can use req.header('Referer');
to get the origin of a request.
You application most likely has a form with the action that points to the login server. This causes the browser to open the URL directly and that is why you get the host of the login server.
Try to add:
var ref = req.header('Referer');
console.log(ref);
If that doesn't work, please provide the part of the front end code that creates the request.
EDIT: The issue is most likely the library that you are using. It is not setting the correct headers. Your request should set Origin header (and Referer header). Try using Axios or Native Fetch API
Example with Fetch:
fetch(URL_TARGET+"/v1/login", {
method: 'post',
body: JSON.stringify({
username: username,
password: password
}
}).then(function(response) {
return response.json();
}).then(function(data) {
console.log(data);
});
Upvotes: 4