Skip to content

Instantly share code, notes, and snippets.

@kylemocode
Created March 11, 2020 09:28
Show Gist options
  • Save kylemocode/7251bec91a53672fa8da3cf63dc675ca to your computer and use it in GitHub Desktop.
Save kylemocode/7251bec91a53672fa8da3cf63dc675ca to your computer and use it in GitHub Desktop.
import { Request } from 'express';
export const getClientIp = function (req: Request) {
const ipInfo: any = req.socket.address();
let ipAddress = ipInfo.address;
if (!ipAddress) {
return '';
}
// convert from "::ffff:192.0.0.1" to "192.0.0.1"
if (ipAddress.substr(0, 7) == "::ffff:") {
ipAddress = ipAddress.substr(7)
}
return ipAddress;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment