Upgrade for newer node versions

Updated packages, fixed breaking changes
Added a seperate control page
Cleaned up unused code
This commit is contained in:
Albert Stefanov 2023-10-25 20:38:34 +03:00
parent 98c7db08b0
commit aea34a1d5c
10 changed files with 93 additions and 82 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
node_modules
node_modules/
package-lock.json

111
app.js
View File

@ -1,111 +1,116 @@
var express = require('express'),
app = module.exports = express.createServer(express.logger()),
io = require('socket.io').listen(app);
app = module.exports = express(),
server = require('http').createServer(app),
Stopwatch = require('./models/stopwatch'),
routes = require('./routes');
errorHandler = require('errorhandler'),
expressLayouts = require('express-ejs-layouts'),
io = require('socket.io')(server, {});
// Configuration
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.configure('development', function() {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.use(expressLayouts);
app.use(express.static(__dirname + '/public'));
app.configure('production', function() {
app.use(express.errorHandler());
});
if (process.env.NODE_ENV === 'development') {
app.use(errorHandler({ dumpExceptions: true, showStack: true }));
}
// Heroku won't actually allow us to use WebSockets
// so we have to setup polling instead.
// https://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
if (process.env.NODE_ENV === 'production') {
app.use(errorHandler());
}
// Routes
// 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';
app.listen(port, host, function() {
console.log("Express server listening on %j in %s mode", app.address(), app.settings.env);
});
// configure title
app.title = process.env.TITLE || 'Timer'
app.get('/', routes.index);
server.listen(port, host, function() {
console.log("Express server listening on %j in %s mode", server.address(), app.settings.env);
});
var stopwatch = new Stopwatch();
stopwatch.on('tick:stopwatch', function(time) {
io.sockets.emit('time', { time: time });
});
io.sockets.emit('time', { time: time });
});
stopwatch.on('reset:stopwatch', function(time) {
io.sockets.emit('time', { time: time });
});
io.sockets.emit('time', { time: time });
});
stopwatch.start();
io.sockets.on('connection', function (socket) {
io.sockets.emit('time', { time: stopwatch.getTime() });
io.sockets.emit('time', { time: stopwatch.getTime() });
socket.on('click:start', function () {
stopwatch.start();
});
socket.on('click:start', function () {
stopwatch.start();
});
socket.on('click:stop', function () {
stopwatch.stop();
});
socket.on('click:stop', function () {
stopwatch.stop();
});
socket.on('click:zero', function () {
stopwatch.zero();
});
socket.on('click:zero', function () {
stopwatch.zero();
});
socket.on('click:reset', function () {
stopwatch.reset();
});
socket.on('click:reset', function () {
stopwatch.reset();
});
socket.on('click:resetShort', function () {
stopwatch.resetShort();
});
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 });
});
app.get('/control/', function(req, res) {
res.render('control', { title: process.env.TITLE });
});
app.post('/reset/', function (req, res) {
stopwatch.reset();
res.send("OK");
});
app.post('/reset-short/', function (req, res) {
stopwatch.resetShort();
res.send("OK");
});
app.post('/start-from-reset/', function (req, res) {
stopwatch.reset();
stopwatch.start();
res.send("OK");
});
app.post('/start-from-reset-short/', function (req, res) {
stopwatch.resetShort();
stopwatch.start();
res.send("OK");
});
app.post('/start/', function (req, res) {
stopwatch.start();
res.send("OK");
});
app.post('/stop/', function (req, res) {
stopwatch.stop();
res.send("OK");
});
app.post('/zero/', function (req, res) {
stopwatch.zero();
res.send("OK");

View File

@ -25,7 +25,7 @@ function Stopwatch() {
// Use Underscore to bind all of our methods
// to the proper context
_.bindAll(this);
_.bindAll(this, 'start', 'stop', 'zero', 'reset', 'resetShort', 'onTick');
};
// ---------------------------------------------

View File

@ -3,12 +3,14 @@
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "~2.5.8",
"ejs": "~0.7.1",
"socket.io": "~0.9.6",
"underscore": "~1.3.3"
},
"engines": {
"node": "0.6.x"
}
"ejs": "^3.1.9",
"errorhandler": "^1.5.1",
"express": "^4.18.2",
"express-ejs-layouts": "^2.5.1",
"socket.io": "^4.7.2",
"underscore": "^1.13.6"
},
"engines": {
"node": "0.6.x"
}
}

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,5 @@
var socket = io.connect(window.location.hostname);
//var socket = io.connect(window.location.hostname);
var socket = io();
socket.on('time', function (data) {
$('#countdown').html(data.time);

View File

@ -1,9 +0,0 @@
var app = require('../app');
/*
* GET home page.
*/
exports.index = function(req, res) {
res.render('index', {"title": app.title})
};

10
views/control.ejs Normal file
View File

@ -0,0 +1,10 @@
<div id="wrapper">
<h1><%= title %></h1>
<div id="countdown"></div>
<button id="start" class="thoughtbot">Start</button>
<button id="stop" class="thoughtbot">Stop</button>
<button id="reset" class="thoughtbot">Reset</button>
<button id="unused2" class="thoughtbot">&nbsp;</button>
<button id="zero" class="thoughtbot">&nbsp;</button>
<button id="resetShort" class="thoughtbot">Reset Short</button>
</div>

View File

@ -1,9 +1,3 @@
<div id="wrapper">
<div id="countdown"></div>
<button id="start" class="thoughtbot">Start</button>
<button id="stop" class="thoughtbot">Stop</button>
<button id="reset" class="thoughtbot">Reset</button>
<button id="unused2" class="thoughtbot">&nbsp;</button>
<button id="zero" class="thoughtbot">&nbsp;</button>
<button id="resetShort" class="thoughtbot">Reset Short</button>
</div>

View File

@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8">
<title><%= title =></title>
<title><%= title %></title>
<meta name="description" content="">
<meta name="author" content="">