timer/app.js

118 lines
2.8 KiB
JavaScript
Raw Normal View History

2023-10-26 08:29:45 +03:00
const
express = require('express'),
app = module.exports = express(),
server = require('http').createServer(app),
Stopwatch = require('./models/stopwatch'),
errorHandler = require('errorhandler'),
expressLayouts = require('express-ejs-layouts'),
io = require('socket.io')(server, {});
2012-06-05 08:45:50 +03:00
// Configuration
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
2012-06-05 08:45:50 +03:00
app.use(expressLayouts);
app.use(express.static(__dirname + '/public'));
2012-06-05 08:45:50 +03:00
if (process.env.NODE_ENV === 'development') {
app.use(errorHandler({ dumpExceptions: true, showStack: true }));
}
2012-06-05 08:45:50 +03:00
if (process.env.NODE_ENV === 'production') {
app.use(errorHandler());
}
2012-06-05 08:54:10 +03:00
2012-06-05 08:45:50 +03:00
// Use the port that Heroku provides or default to 5000
var port = process.env.PORT || 5000;
var host = process.env.HOST || '0.0.0.0';
2012-06-05 08:45:50 +03:00
server.listen(port, host, function() {
2023-10-26 08:29:45 +03:00
console.log("Express server listening on %j in %s mode", server.address(), app.settings.env);
});
var stopwatch = new Stopwatch();
2023-10-26 08:29:45 +03:00
stopwatch.on('tick:stopwatch', function(time) {
2023-10-26 08:29:45 +03:00
io.sockets.emit('time', { time: time });
});
stopwatch.on('reset:stopwatch', function(time) {
2023-10-26 08:29:45 +03:00
io.sockets.emit('time', { time: time });
});
stopwatch.start();
2012-06-05 08:45:50 +03:00
io.sockets.on('connection', function (socket) {
2023-10-26 08:29:45 +03:00
io.sockets.emit('time', { time: stopwatch.getTime() });
2023-10-26 08:29:45 +03:00
socket.on('click:start', function () {
stopwatch.start();
});
2023-10-26 08:29:45 +03:00
socket.on('click:stop', function () {
stopwatch.stop();
});
2023-10-26 08:29:45 +03:00
socket.on('click:zero', function () {
stopwatch.zero();
});
2023-10-26 08:29:45 +03:00
socket.on('click:reset', function () {
stopwatch.reset();
});
2023-10-26 08:29:45 +03:00
socket.on('click:resetShort', function () {
stopwatch.resetShort();
});
});
// configure title
//app.use(function (req, res, next) {
// res.locals.title = process.env.TITLE || 'Timer'
//});
app.get('/', function(req, res) {
res.render('index', { title: process.env.TITLE });
});
2023-10-26 08:29:45 +03:00
const control = express.Router();
control.get('/', function(req, res) {
res.render('control', { title: process.env.TITLE });
});
2023-10-26 08:29:45 +03:00
control.post('/reset/', function (req, res) {
stopwatch.reset();
res.send("OK");
});
2023-10-26 08:29:45 +03:00
control.post('/reset-short/', function (req, res) {
stopwatch.resetShort();
res.send("OK");
});
2023-10-26 08:29:45 +03:00
control.post('/start-from-reset/', function (req, res) {
stopwatch.reset();
stopwatch.start();
res.send("OK");
});
2023-10-26 08:29:45 +03:00
control.post('/start-from-reset-short/', function (req, res) {
stopwatch.resetShort();
stopwatch.start();
res.send("OK");
});
2023-10-26 08:29:45 +03:00
control.post('/start/', function (req, res) {
stopwatch.start();
res.send("OK");
});
2023-10-26 08:29:45 +03:00
control.post('/stop/', function (req, res) {
stopwatch.stop();
res.send("OK");
});
2023-10-26 08:29:45 +03:00
control.post('/zero/', function (req, res) {
stopwatch.zero();
res.send("OK");
});
2023-10-26 08:29:45 +03:00
app.use('/control', control);