8

is it possible to get hostname in Node.js?

This is how I get client's IP:

var ip = request.header('x-forwarded-for');

So, how do I get client's hostname?

var hostname = request.header('???');

Thanks for reply!

4
  • 2
    What makes you think they have one? :) Commented Nov 23, 2010 at 10:54
  • So not everybody has hostname? Commented Nov 23, 2010 at 10:57
  • 1
    definitely not, that's a reverse DNS lookup, and everyone may or may not have one. Commented Nov 23, 2010 at 11:02
  • 2
    Actually, even if HTTPD have this ability, it would be disabled in realworld installations. Because its slow. BTW, X-FORWARDED-FOR is proxy header, correct one is REMOTE_ADDR. (and REMOTE_HOST in case of hostname lookups) Commented Nov 23, 2010 at 11:24

6 Answers 6

16

You can use the 'dns' module to do a reverse dns lookup:

require('dns').reverse('12.12.12.12', function(err, domains) {
    if(err) {
        console.log(err.toString());
        return;
    }
    console.log(domains);
});

See: http://nodejs.org/docs/v0.3.1/api/all.html#dns.reverse

Sign up to request clarification or add additional context in comments.

Comments

14

I think this might help you. That's not exactly the client hostname but the ip address.

function getClientAddress(req) {
  return req.headers['x-forwarded-for'] || req.connection.remoteAddress;
}

3 Comments

not true, it uses the remote address if the connection wasn't proxied
You should make that clearer in your post, but +1 all the same
Solves the issue for local development and having the app on Heroku behind a reverse proxy! :)
6

I think the only way you can do it is like this:

<form method="post" action="/gethostname">
    <label for="hostname">What is your hostname?</label>
    <input type="text" name="hostname" id="hostname">
</form>

But I would suggest you don't really need it, it's not like you can do anything useful with the information. If you just want a string to identify with the user's machine then you can make something up.

If what you're really after is the FQDN then I would suggest it's still not really that useful to you, but for that you need Reverse DNS lookup. If you're on a VPS or similar you can probably configure your box to do this for you, but note that it'll likely take a few seconds so it's not a good idea to do it as part of a response. Also note, you'll not be getting the user's machine's FQDN in most cases but that of their router.

Comments

0

You can also achieve the same if you're using socket.io in the following manner:

// Setup an example server
var server = require('socket.io').listen(8080);

// On established connection
server.sockets.on('connection', function (socket) {

    // Get server host
    var host = socket.handshake.headers.host;

    // Remove port number together with colon
    host = host.replace(/:.*$/,"");

    // To test it, output to console
    console.log(host);
});

Comments

-1

if you are using express,

then you can do as follows,

var express = require("express"); 
var app = express.createServer();
app.listen(8080);

app.get("/", function (req, res){
    console.log("REQ:: "+req.headers.host);
    res.end(req.headers.host);
});

2 Comments

As explained before, if the request has been proxied, the host header don't have the correct data.
When I try this, req.headers.host contains the host name of the server right down to the "8080" port being used.
-1

You can get hostname from the os module:

var os = require("os"); 
os.hostname();

1 Comment

This gives server's hostname.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.