Skip to content

Instantly share code, notes, and snippets.

@xxshady
Last active December 7, 2021 21:32
Show Gist options
  • Save xxshady/5c06362ad63e2048266eb20f41877a2f to your computer and use it in GitHub Desktop.
Save xxshady/5c06362ad63e2048266eb20f41877a2f to your computer and use it in GitHub Desktop.
external ip nodejs
import https from "https"
const options = {
hostname: "ipinfo.io",
path: "/ip",
method: "GET",
}
export const getExternalIp = (): Promise<string> =>
new Promise<string>((resolve, reject) => {
const req = https.request(options, res => {
if (res.statusCode !== 200) {
reject(new Error("failed get ip"))
return
}
res.on("data", (ipAddr: Buffer) => {
try {
resolve(ipAddr.toString())
} catch (e) {
reject(e)
}
})
})
req.on("error", reject)
req.end()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment