From a00a8002bb1d73c62dd775b1fd03cbad0aefcd8a Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Fri, 30 Dec 2016 07:22:12 +0000 Subject: [PATCH] Improve logging --- package.json | 1 + server.js | 18 +++++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 432109f..2616b9c 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "dependencies": { "body-parser": "^1.15.2", "freezer-js": "^0.6.0", + "moment": "^2.17.1", "qrcode.react": "^0.6.1", "react": "^0.13.0", "react-router": "^0.13.3", diff --git a/server.js b/server.js index cc100d7..2e9558f 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,7 @@ const path = require('path'); const express = require('express'); const bodyParser = require('body-parser'); +const moment = require('moment'); const app = express(); @@ -9,6 +10,10 @@ const port = 3000; app.use(bodyParser.urlencoded({ extended: false })); +function log(message) { + console.log(moment().format() + ': ' + message); +} + app.use('/', express.static(path.join(__dirname, 'public'))); app.get('/*', (req, res) => { res.sendFile(path.join(__dirname, 'public/index.html')); @@ -21,18 +26,20 @@ app.post('*', (req, res) => { if (data && data.substring(0,2) === 'd:') { let message = data.substring(2); - console.log("to: " + id); - console.log(message); + log('[NOTICA] Message sent to ' + id + ': ' + message); io.in(id).emit('message', message); res.end(); + } else { + log('Ignoring bad POST data to: ' + id); + res.send('Bad POST data.'); } }); const server = app.listen(port, 'localhost', (err) => { if (err) { - console.log(err); + log('[ERROR] Server error: ' + err); return; } console.info('==> Listening on port %s. Open up %s:%s/ in your browser.', port, host, port); @@ -41,11 +48,8 @@ const server = app.listen(port, 'localhost', (err) => { const io = require('socket.io').listen(server); io.on('connection', (socket) => { - console.log("new connection!"); socket.on('room', (room) => { - console.log("telling it to join room."); + log('New connection joining room: ' + room); socket.join(room); }); - - });