search

Home  >  Q&A  >  body text

node.js - nodejs express如何获取服务器IP和端口?

我们的网址中常用<base href="">来标明前面那部分,然而,nodejs express框架有这个方法吗?是不是要手动写一个拦截器?

天蓬老师天蓬老师2778 days ago976

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-17 15:26:09

    • What are you trying to intercept? Your node express should not be a server, listening for requests. The requests are all sent to your server. Do you know your own IP and port? ? ?

    const server = app.listen(3000, 'localhost', function () {
      const host = server.address().address
      const port = server.address().port
      console.log("应用实例,访问地址为 http://%s:%s", host, port)
    })
    

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 15:26:09

    var ip = req.headers['x-real-ip'] ? req.headers['x-real-ip'] : req.ip.replace(/::ffff:/, '');

    The IP obtained above has been processed. You can get the IP regardless of whether you use a proxy or a direct connection.

    Port. . . It seems I haven’t found a way

    reply
    0
  • Cancelreply