This is my problem: I have an application that runs NodeJS in server-side, JavaScript front-side. Users log in from different machines to the web application (obviously) and I need to know what machines they are logging themselves from. Is there any way to know this information with JavaScript or one of its frameworks and libraries? I know there's "os"
and its method hostname()
to know this with NodeJS server-side, but this would return the machine name of the server.

- 3,048
- 4
- 25
- 44
-
Can't you just run those methods client side and send the data to the server? – Charlie Fish Jul 20 '16 at 19:41
-
@CharlieFish: how would I do that? Can I inject NodeJS code client-side? – Heathcliff Jul 20 '16 at 19:41
-
Oh wait OS is a Node package. Is hostname built into js or is it Node specific? – Charlie Fish Jul 20 '16 at 19:43
-
No, it's Node specific. "os" is a library one can reference in Node – Heathcliff Jul 20 '16 at 19:43
-
Hmm. Yeah I haven't tried to do this before but my best guess would be to find some type of package or method for client side JS that would get you that information then you can send that to the node server. Possible someone would know if such a method or command exists I'm not sure tho. – Charlie Fish Jul 20 '16 at 19:45
-
Just fyi certain node only modules (along with your own server side code) can be used client side with libraries such as browserify to compile your JavaScript. Great way of sharing the same code on both sides of the wire. – ste2425 Jul 20 '16 at 19:57
4 Answers
The server will know the IP address that the HTTP request is coming from whatever API you are using to host your server in Node.JS is very likely to expose it (ExpressJS has request.ip
). You can then perform a reverse DNS look up to get the hostname.
That will give you the name, assuming that the machines are connecting directly to your server and are not being NAT or a proxy.
Failing that, the closet you could come would be to use client-side JS to discover the IP address and then send it to the server with (for example) Ajax.
-
`request.hostname` returns the IP of the server where the application is running. Or `localhost`, when run locally – Heathcliff Jul 20 '16 at 20:04
-
-
performing a reverse dns lookup will get you the hostname. But the original question is to get the *machine name*. Please advise how to get the machine name in the same context – Hugobop Oct 28 '22 at 16:21
-
You'll need to get the client to send it to you.
I'm not aware of a method in the browser that will get the machine name, but if the request is coming from another server, you can have that server use os
to send the machine name (in say, some custom header X-Client-Machine-Name
or whatever cool name you come up with...)
If the request is coming from a browser, an option is to just look at the IP of the client who is sending you a request (see How to determine a user's IP address in node). This is useful if your machines are all in a private network and the IPs don't change often. However, given that the default for most networks is dynamic IPs, you might run into some configuration issues on that front (but it should be do-able). You might have to maintain some table of IP->Machine Name mappings (this could be built dynamically using some daemon on each machine that you're expecting to send requests to your server that updates your table on startup). That's a bit laborious though.
-
The IPs are dynamic, and there's little chance of asking the client to keep tables updated with mappings of IPs - Machine Names. The requests will be coming from a browser, not another server. – Heathcliff Jul 21 '16 at 13:30
Not sure what your use case is, but you could use the session ID to know which browser session they are using. Barring that, you could use something like Fingerprintjs2:
https://github.com/Valve/fingerprintjs2
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/fingerprintjs2/1.4.1/fingerprint2.min.js"></script>
JS:
new Fingerprint2().get(function(result, components) {
// do something with result
});

- 975
- 6
- 4
To get ip Address of the client system or of any system you can do this.
Change req to whatever nomenclatural you are using
var ip = (req.headers["X-Forwarded-For"] || req.headers["x-forwarded-for"] || '').split(',')[0] || req.connection.remoteAddress;
console.log("ip address is:- " + ip);
To get the host name of the client system or of any system you can do this
const os = require('os');
var client_machine_name = os.hostname();
console.log("Client Machine Name is:- " + client_machine_name);
This will work on both js and nodejs

- 11
- 1