This repository has been archived on 2024-06-03. You can view files and clone it, but cannot push or open issues or pull requests.
quickstatus/server.js

42 lines
860 B
JavaScript
Raw Normal View History

2019-03-01 19:44:08 +00:00
const express = require('express')
const cprog = require('child_process')
const path = require('path')
const app = express()
const port = process.env.PORT || 8432
/* Information segment */
let info = {}
let infoTimer
async function requestInfo () {
return new Promise((resolve, reject) => {
let proc = cprog.exec('node ' + path.join(__dirname, 'system.js'), (err, stdout) => {
if (err) return reject(err)
resolve(JSON.parse(stdout))
})
})
}
function update () {
requestInfo().then((newInfo) => {
info = newInfo
}).catch(e => console.error(e))
}
infoTimer = setInterval(update, 8000)
update()
/* Web server segment */
app.use('/status', function (req, res) {
res.jsonp(info)
})
app.use('/', express.static(path.join(__dirname, 'public')))
app.listen(port, function (argument) {
console.log('Listening on', port)
})