-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
82 lines (75 loc) · 2.94 KB
/
server.js
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
//server.js
const express = require('express')
const app = express()
const port = 8080
const os = require('os')
const request = require('sync-request') // Required to figure out the public IP
const defaultGateway = require('default-gateway'); // Required to find default gateway
const zeitFormat = require('./zeitFormat.js'); //Required to format time in second to d/h/m/s
// Set the start time for the container as a constant
const startTime = os.uptime()
// To use the Pug template engine
app.set('view engine', 'pug')
// To serve static files
app.use(express.static('public'))
// Provides the environment data as JSON object via REST
app.get(['/json','/rest'], function (req, res) {
// let remoteAddress = req.connection.remoteAddress.split(':')[3];
res.send(environmentData(req))
})
// Provides the environment data as JSON object via HTTP
app.get(['/httpjson','/httprest'], function (req, res) {
res.render('http_json', {
environmentData: JSON.stringify(environmentData(req))
})
})
// Provides the environment data as user friendly HTML via HTTP
app.get(['/','/index.html'], function (req, res) {
res.render('index', {
environmentData: environmentData(req)
})
})
//////////////////////////////////////////////////////////////////////
// Collects the environment data
function environmentData(req) {
return {
//socket
// https://nodejs.org/api/net.html#net_class_net_socket
socketLocalAddress: req.connection.localAddress.split(':')[3],
socketLocalPort: req.connection.localPort,
socketRemoteAddress: req.connection.remoteAddress.split(':')[3],
socketRemotePort: req.connection.remotePort,
socketRemoteFamily: req.connection.remoteFamily,
socketBytesRead: req.connection.bytesRead,
socketBytesWritten: req.connection.bytesWritten,
socketBufferSize: req.connection.bufferSize,
//default gateway
//https://www.npmjs.com/package/default-gateway
defaultGateway_v4: defaultGateway.v4.sync(),
// defaultGateway_v6: defaultGateway.v6.sync(), Funktioniert leider nicht "Error: Unable to determine default gateway"
//server
serverAddress: req.connection.address(),
//req
// https://www.tutorialspoint.com/nodejs/nodejs_request_object.htm
reqOriginalUrl: req.originalUrl,
reqRoute: req.route,
reqQuery: req.query,
//
publicIP: request('GET', 'https://api.ipify.org').getBody('utf8'), // Figure out the public IP
//os
//https://nodejs.org/api/os.html
osHostname: os.hostname(),
osNetworkInterfaces: os.networkInterfaces(),
osPlatform: os.platform(),
osRelease: os.release(),
osCpus: os.cpus(),
osTotalmem: os.totalmem(),
osFreemem: os.freemem(),
osUptime: os.uptime()-startTime,
osUptimeLong: zeitFormat.uptime(os.uptime()-startTime),
osUserInfo: os.userInfo(),
osHomedir: os.homedir(),
}
}
//////////////////////////////////////////////////////////////////////////////////
app.listen(port, () => console.log(`App listening on port ${port}!`))