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

75
app.js
View File

@ -1,58 +1,43 @@
var express = require('express'), var express = require('express'),
app = module.exports = express.createServer(express.logger()), app = module.exports = express(),
io = require('socket.io').listen(app); server = require('http').createServer(app),
Stopwatch = require('./models/stopwatch'), Stopwatch = require('./models/stopwatch'),
routes = require('./routes'); errorHandler = require('errorhandler'),
expressLayouts = require('express-ejs-layouts'),
io = require('socket.io')(server, {});
// Configuration // Configuration
app.configure(function() { app.set('views', __dirname + '/views');
app.set('views', __dirname + '/views'); app.set('view engine', 'ejs');
app.set('view engine', 'ejs');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function() { app.use(expressLayouts);
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); app.use(express.static(__dirname + '/public'));
});
app.configure('production', function() { if (process.env.NODE_ENV === 'development') {
app.use(express.errorHandler()); app.use(errorHandler({ dumpExceptions: true, showStack: true }));
}); }
// Heroku won't actually allow us to use WebSockets if (process.env.NODE_ENV === 'production') {
// so we have to setup polling instead. app.use(errorHandler());
// 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);
});
// Routes
// Use the port that Heroku provides or default to 5000 // Use the port that Heroku provides or default to 5000
var port = process.env.PORT || 5000; var port = process.env.PORT || 5000;
var host = process.env.HOST || '0.0.0.0'; 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(); 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 });
}); });
stopwatch.on('reset:stopwatch', function(time) { stopwatch.on('reset:stopwatch', function(time) {
io.sockets.emit('time', { time: time }); io.sockets.emit('time', { time: time });
}); });
stopwatch.start(); stopwatch.start();
@ -80,32 +65,52 @@ io.sockets.on('connection', function (socket) {
}); });
}); });
// 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) { app.post('/reset/', function (req, res) {
stopwatch.reset(); stopwatch.reset();
res.send("OK"); res.send("OK");
}); });
app.post('/reset-short/', function (req, res) { app.post('/reset-short/', function (req, res) {
stopwatch.resetShort(); stopwatch.resetShort();
res.send("OK"); res.send("OK");
}); });
app.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");
}); });
app.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");
}); });
app.post('/start/', function (req, res) { app.post('/start/', function (req, res) {
stopwatch.start(); stopwatch.start();
res.send("OK"); res.send("OK");
}); });
app.post('/stop/', function (req, res) { app.post('/stop/', function (req, res) {
stopwatch.stop(); stopwatch.stop();
res.send("OK"); res.send("OK");
}); });
app.post('/zero/', function (req, res) { app.post('/zero/', function (req, res) {
stopwatch.zero(); stopwatch.zero();
res.send("OK"); res.send("OK");

View File

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

View File

@ -3,10 +3,12 @@
"version": "0.0.1", "version": "0.0.1",
"private": true, "private": true,
"dependencies": { "dependencies": {
"express": "~2.5.8", "ejs": "^3.1.9",
"ejs": "~0.7.1", "errorhandler": "^1.5.1",
"socket.io": "~0.9.6", "express": "^4.18.2",
"underscore": "~1.3.3" "express-ejs-layouts": "^2.5.1",
"socket.io": "^4.7.2",
"underscore": "^1.13.6"
}, },
"engines": { "engines": {
"node": "0.6.x" "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) { socket.on('time', function (data) {
$('#countdown').html(data.time); $('#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="wrapper">
<div id="countdown"></div> <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> </div>

View File

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