Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote IP from request on http server

Tags:

node.js

is there any way to know the remote IP adress form a request on on the http server? Using "net" for a socket connection is socket.remoteAddress but for the http server I get undifined for the same .remoteAddress

Regards

like image 225
user1031782 Avatar asked Nov 12 '11 04:11

user1031782


People also ask

Can you get IP address from http request?

You can use RemoteAddr to get the remote client's IP address and port (the format is "IP:port"), which is the address of the original requestor or the last proxy (for example a load balancer which lives in front of your server). This is all you have for sure. This is because internally http. Header.

How do I send an IP address to an HTTP request?

The –r junction option allows you to insert client IP address information into the HTTP headers of requests destined for junctioned application servers. The HTTP header information enables applications on junctioned third-party servers to perform actions based on this IP address information.

What is remote address in HTTP request?

REMOTE_HOST pertains to the hostname of the client (i.e. the computer making the request). REMOTE_ADDR refers to the IP address of the client. There would be times when the hostname is unresolvable so the REMOTE_HOST will return the REMOTE_ADDR or the IP address instead.


2 Answers

I'm just adding this because I came across this article and it still took me a little bit of time to figure out it was as you see in the following:

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello ' + req.connection.remoteAddress + '!');
    console.log("request received from: " + req.connection.remoteAddress);
}).listen(1337, '127.0.0.1');

console.log('Server running at http://127.0.0.1:1337/');
like image 103
JustTrying Avatar answered Oct 16 '22 09:10

JustTrying


You can use the request.connection method to retrieve the net.Socket object, where you can then use your socket.remoteAddress property.

like image 25
sarnold Avatar answered Oct 16 '22 07:10

sarnold