Get hostname of current request in node.js Express

后端 未结 5 1996
礼貌的吻别
礼貌的吻别 2020-11-28 20:32

So, I may be missing something simple here, but I can\'t seem to find a way to get the hostname that a request object I\'m sending a response to was requested from.

5条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 21:19

    You can use the os Module:

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

    See http://nodejs.org/docs/latest/api/os.html#os_os_hostname

    Caveats:

    1. if you can work with the IP address -- Machines may have several Network Cards and unless you specify it node will listen on all of them, so you don't know on which NIC the request came in, before it comes in.

    2. Hostname is a DNS matter -- Don't forget that several DNS aliases can point to the same machine.

提交回复
热议问题