45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
// Importing express module
|
|
const express = require("express");
|
|
|
|
const port = 8080;
|
|
const app = express();
|
|
const { networkInterfaces } = require('os');
|
|
const nets = networkInterfaces();
|
|
const results = Object.create(null); // Or just '{}', an empty object
|
|
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({ extended: true }));
|
|
app.use(express.static('public'));
|
|
|
|
for (const name of Object.keys(nets)) {
|
|
for (const net of nets[name]) {
|
|
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
|
|
// 'IPv4' is in Node <= 17, from 18 it's a number 4 or 6
|
|
const familyV4Value = typeof net.family === 'string' ? 'IPv4' : 4
|
|
if (net.family === familyV4Value && !net.internal) {
|
|
if (!results[name]) {
|
|
results[name] = [];
|
|
}
|
|
results[name].push(net.address);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Handling the get request
|
|
app.get("/", (req, res) => {
|
|
res.send("Hello World");
|
|
});
|
|
|
|
app.get("/ip", (req, res) => {
|
|
res.send(results);
|
|
});
|
|
|
|
// Starting the server on the 80 port
|
|
app.listen(port, () => {
|
|
console.log(`The application started
|
|
successfully on port ${port}`);
|
|
console.log(`IP address ${JSON.stringify(results)}`);
|
|
});
|
|
|
|
module.exports = app
|