Skip to content

Instantly share code, notes, and snippets.

@BananaAcid
Last active April 23, 2025 15:46
Show Gist options
  • Save BananaAcid/f156e9248c2d4e686dc61bc24d058307 to your computer and use it in GitHub Desktop.
Save BananaAcid/f156e9248c2d4e686dc61bc24d058307 to your computer and use it in GitHub Desktop.
simple hostname info in nodejs
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