Last active
February 18, 2022 07:58
-
-
Save HUGHNew/1c88058cf2abf9aea2d88d1de96fab7d to your computer and use it in GitHub Desktop.
exmaple server to fetch public ip address
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
| // get your public ip of the subnet | |
| // base code fetch from <https://nodejs.dev/learn/build-an-http-server> | |
| // how to run? : `node ./GetPublicIpAddrServer.js` | |
| const http = require('http') | |
| const port4 = 12345 | |
| const port6 = 12346 | |
| const svn_fn = (req, res) => { | |
| res.statusCode = 200 | |
| res.setHeader('Content-Type', 'text/html') | |
| // content = req.socket.remoteFamily + "|" + req.socket.remoteAddress + "|" + req.socket.remotePort | |
| content = `${req.socket.remoteAddress}:${req.socket.remotePort}` | |
| res.end(content) | |
| } | |
| const v4 = http.createServer(svn_fn) | |
| const v6 = http.createServer(svn_fn) | |
| v4.listen(port4,"0.0.0.0", () => { | |
| console.log(`IPv4 Server running at port ${port4}`) | |
| }) | |
| v6.listen(port6, () => { | |
| console.log(`IPv6 Server running at port ${port6}`) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment