File size: 2,903 Bytes
8c2369a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | const express = require('express');
const os = require('os');
const process = require('process');
const geoip = require('geoip-lite');
const app = express();
const port = 3000;
// Set JSON response indentation to 4 spaces
app.set('json spaces', 4);
// Function to convert bytes to binary MiB without decimals
const bytesToBinaryMiB = (bytes) => Math.floor(bytes / (1024 * 1024)) + 'MiB';
// Function to convert uptime in seconds to "days hours minutes seconds"
const secondsToDhms = (seconds) => {
seconds = Number(seconds);
const d = Math.floor(seconds / (3600 * 24));
const h = Math.floor((seconds % (3600 * 24)) / 3600);
const m = Math.floor((seconds % 3600) / 60);
const s = Math.floor(seconds % 60);
return `${d} days ${h} hours ${m} minutes ${s} seconds`;
};
// Root endpoint
app.get('/', (req, res) => {
const runtime = secondsToDhms(process.uptime());
res.setHeader('Content-Type', 'application/json');
res.status(200).send(JSON.stringify({
status: 200,
message: `Server is running, uptime: ${runtime}`
}, null, 4)); // JSON response with 4 spaces indentation
});
// Specs endpoint
app.get('/specs', (req, res) => {
const networkInterfaces = os.networkInterfaces();
const specs = {
system: {
platform: os.platform(),
release: os.release(),
arch: os.arch(),
uptime: secondsToDhms(os.uptime()),
loadavg: os.loadavg()
},
memory: {
totalmem: bytesToBinaryMiB(os.totalmem()),
freemem: bytesToBinaryMiB(os.freemem())
},
cpu: {
cpus: os.cpus()
},
network: {
networkInterfaces: networkInterfaces,
ipv4Info: {}
},
user: {
homedir: os.homedir(),
hostname: os.hostname(),
userInfo: os.userInfo()
},
process: {
pid: process.pid,
version: process.version,
title: process.title,
argv: process.argv,
memoryUsage: process.memoryUsage(),
cwd: process.cwd(),
execPath: process.execPath,
platform: process.platform,
uptime: secondsToDhms(process.uptime()),
env: process.env
}
};
// Extract IPv4 info for each network interface
Object.keys(networkInterfaces).forEach((key) => {
specs.network.ipv4Info[key] = networkInterfaces[key]
.filter((iface) => iface.family === 'IPv4')
.map((iface) => {
const geo = geoip.lookup(iface.address);
return {
address: iface.address,
netmask: iface.netmask,
mac: iface.mac,
internal: iface.internal,
country: geo ? geo.country : null,
region: geo ? geo.region : null,
city: geo ? geo.city : null,
ll: geo ? geo.ll : null
};
});
});
res.setHeader('Content-Type', 'application/json');
res.send(specs);
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
|