Move control socket

This commit is contained in:
Albert Stefanov 2023-10-26 08:29:45 +03:00
parent 48701890df
commit bc8b805b2f
1 changed files with 39 additions and 38 deletions

33
app.js
View File

@ -1,4 +1,5 @@
var express = require('express'), const
express = require('express'),
app = module.exports = express(), app = module.exports = express(),
server = require('http').createServer(app), server = require('http').createServer(app),
Stopwatch = require('./models/stopwatch'), Stopwatch = require('./models/stopwatch'),
@ -30,7 +31,9 @@ var host = process.env.HOST || '0.0.0.0';
server.listen(port, host, function() { server.listen(port, host, function() {
console.log("Express server listening on %j in %s mode", server.address(), app.settings.env); console.log("Express server listening on %j in %s mode", server.address(), app.settings.env);
}); });
var stopwatch = new Stopwatch(); var stopwatch = new Stopwatch();
stopwatch.on('tick:stopwatch', function(time) { stopwatch.on('tick:stopwatch', function(time) {
io.sockets.emit('time', { time: time }); io.sockets.emit('time', { time: time });
}); });
@ -75,43 +78,41 @@ app.get('/', function(req, res) {
res.render('index', { title: process.env.TITLE }); res.render('index', { title: process.env.TITLE });
}); });
app.get('/control/', function(req, res) {
const control = express.Router();
control.get('/', function(req, res) {
res.render('control', { title: process.env.TITLE }); res.render('control', { title: process.env.TITLE });
}); });
control.post('/reset/', function (req, res) {
app.post('/reset/', function (req, res) {
stopwatch.reset(); stopwatch.reset();
res.send("OK"); res.send("OK");
}); });
control.post('/reset-short/', function (req, res) {
app.post('/reset-short/', function (req, res) {
stopwatch.resetShort(); stopwatch.resetShort();
res.send("OK"); res.send("OK");
}); });
control.post('/start-from-reset/', function (req, res) {
app.post('/start-from-reset/', function (req, res) {
stopwatch.reset(); stopwatch.reset();
stopwatch.start(); stopwatch.start();
res.send("OK"); res.send("OK");
}); });
control.post('/start-from-reset-short/', function (req, res) {
app.post('/start-from-reset-short/', function (req, res) {
stopwatch.resetShort(); stopwatch.resetShort();
stopwatch.start(); stopwatch.start();
res.send("OK"); res.send("OK");
}); });
control.post('/start/', function (req, res) {
app.post('/start/', function (req, res) {
stopwatch.start(); stopwatch.start();
res.send("OK"); res.send("OK");
}); });
control.post('/stop/', function (req, res) {
app.post('/stop/', function (req, res) {
stopwatch.stop(); stopwatch.stop();
res.send("OK"); res.send("OK");
}); });
control.post('/zero/', function (req, res) {
app.post('/zero/', function (req, res) {
stopwatch.zero(); stopwatch.zero();
res.send("OK"); res.send("OK");
}); });
app.use('/control', control);