Remote-Brightness-PWA/app.js

43 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-12-27 15:39:07 +00:00
const http = require('http'),
fs = require('fs');
const { exec } = require('child_process');
const { parse } = require('querystring');
function setBrightness(display, value, res) {
exec(`setbrightness "${value}"`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
// Redirect the client back to the main page after the command has run
res.writeHead(303, { Location: '/' });
res.end();
});
}
http.createServer((req, res) => {
if (req.url.startsWith('/button_pressed?')) {
// Parse the query string from the request URL
const query = req.url.split('?')[1];
// Read the brightness value from the query string
//const brightness = query.brightness;
// Set the brightness using the value from the slider
setBrightness(1, query, res);
} else if (req.url === '/') {
// Send the form when the root URL is requested
let item = fs.readFile('./index.html', function(err, html) {
if (err) {
throw err;
}
else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(html);
}
});
}
}).listen(5000);
console.log('Server listening on port 5000');