Nodejs Server Hostname

后端 未结 1 2005
我寻月下人不归
我寻月下人不归 2021-02-13 10:02

Ok, so it seems pretty easy in Node.js to get the hostname of the request being made to my server:

app.get(\'/\', function(req,res){
    console.log(req.headers.         


        
1条回答
  •  清歌不尽
    2021-02-13 10:34

    Yes you can using the;

    var express = require('express'),
        app = express(),
        server  = require('http').createServer(app);
    
    server.listen(3000, function(err) {
            console.log(err, server.address());
    });
    

    should print

    { address: '0.0.0.0', family: 'IPv4', port: 3000 }
    

    you can also retreive the hostname for the os by the following;

    require('os').hostname();
    

    0 讨论(0)
提交回复
热议问题