Last active
December 7, 2021 21:32
-
-
Save xxshady/5c06362ad63e2048266eb20f41877a2f to your computer and use it in GitHub Desktop.
external ip nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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