LoginSignup
3
2

More than 5 years have passed since last update.

接続者のリモートアドレスを取得する

Posted at

req.connection.remoteAddressから取得することが可能です。
なお、サブネットも含まれていますので、IPアドレスのみを取得する場合は、
以下のように抜きだすと良いでしょう

 app.get('/', function(req, res) {
    const remoteAddress = req.connection.remoteAddress;

    const splittedAddress = remoteAddress.split(':');
    const ipAddress = splittedAddress[splittedAddress.length - 1];

    res.json({yourIP: ipAddress});
  });
3
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2