Last active
April 23, 2025 15:46
-
-
Save BananaAcid/f156e9248c2d4e686dc61bc24d058307 to your computer and use it in GitHub Desktop.
simple hostname info in 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 { createServer } from 'node:http'; | |
import { execSync } from 'node:child_process'; | |
const server = createServer(async function (req, res) { | |
Object.entries({ | |
'Content-Type': 'application/json', | |
'Access-Control-Allow-Origin': '*', | |
'Access-Control-Allow-Methods': '*', | |
}).forEach(([key, value]) => { | |
res.setHeader(key, value); | |
}); | |
let result = {}; | |
try { | |
result = { | |
hostname: execSync('hostname', { encoding: 'utf8' }).trim(), | |
ip: execSync("ip -4 addr show eth0 | grep -oP '(?<=inet\\s)\\d+(\\.\\d+){3}'", { encoding: 'utf8' }).trim(), | |
}; | |
} | |
catch(err) {} | |
res.writeHead(200); | |
res.end(JSON.stringify(result)); | |
}); | |
let PORT = process.env.NODE_PORT || process.env.PORT || 3000; | |
server.listen(PORT, () => console.log(`Running at ${PORT}`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment