Browse Source

Update dependencies and lint code

master
Peter Cunha 4 years ago
parent
commit
945a44c938
6 changed files with 107 additions and 113 deletions
  1. +3
    -0
      .gitignore
  2. +34
    -34
      app.js
  3. +7
    -7
      package.json
  4. +19
    -19
      public/javascripts/worker.js
  5. +7
    -7
      routes/index.js
  6. +37
    -46
      routes/socket.js

+ 3
- 0
.gitignore View File

@@ -28,3 +28,6 @@ node_modules

# Debug log from npm
npm-debug.log

# Package lock
package-lock.json

+ 34
- 34
app.js View File

@@ -1,49 +1,49 @@
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var socketio = require('socket.io');
var express = require('express')
var path = require('path')
var favicon = require('serve-favicon')
var logger = require('morgan')
var cookieParser = require('cookie-parser')
var bodyParser = require('body-parser')
var socketio = require('socket.io')

var app = express();
var io = socketio();
app.io = io;
var app = express()
var io = socketio()
app.io = io

var index = require('./routes/index');
var socket = require('./routes/socket')(io);
var index = require('./routes/index')
var socket = require('./routes/socket')(io)

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'pug')

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))

app.use('/', index);
app.use('/Pad', socket);
app.use('/', index)
app.use('/Pad', socket)

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use(function (req, res, next) {
var err = new Error('Not Found')
err.status = 404
next(err)
})

// error handler
app.use(function(err, req, res, next) {
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.locals.message = err.message
res.locals.error = req.app.get('env') === 'development' ? err : {}

// render the error page
res.status(err.status || 500);
res.render('error');
});
res.status(err.status || 500)
res.render('error')
})

module.exports = app;
module.exports = app

+ 7
- 7
package.json View File

@@ -6,15 +6,15 @@
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.17.1",
"body-parser": "~1.17.2",
"cookie-parser": "~1.4.3",
"debug": "~2.6.3",
"express": "~4.15.2",
"morgan": "~1.8.1",
"pug": "^2.0.0-beta9",
"serve-favicon": "~2.4.1",
"debug": "~2.6.8",
"express": "~4.15.3",
"morgan": "~1.8.2",
"pug": "^2.0.0-rc.2",
"serve-favicon": "~2.4.3",
"shortid": "^2.2.8",
"socket.io": "^1.7.3"
"socket.io": "^2.0.3"
},
"description": "Collaborative online notepad",
"main": "app.js",

+ 19
- 19
public/javascripts/worker.js View File

@@ -1,35 +1,35 @@
var socket = io();
var cache = "";
var firstLoad = true;
var socket = io()
var cache = ''
var firstLoad = true

var f = window.location.pathname;
var temp = f.split("/");
var pathname;
var f = window.location.pathname
var temp = f.split('/')
var pathname

if (f.substr(f.length - 1) == "/") {
pathname = temp[temp.length - 2];
if (f.substr(f.length - 1) == '/') {
pathname = temp[temp.length - 2]
} else {
pathname = temp[temp.length - 1];
pathname = temp[temp.length - 1]
}

// Ask server latest data
socket.emit('sync', { path: pathname });
socket.emit('sync', { path: pathname })

// Set TextArea as server response
socket.on('notify', function (data) {
if (data.path == pathname) {
document.getElementById("text").value = data.content;
document.getElementById('text').value = data.content
} else {
console.log("Recieved: " + data.path + " Have: " + pathname);
console.log('Recieved: ' + data.path + ' Have: ' + pathname)
}
});
})

// Send latest data to server
function processText() {
var x = document.getElementById("text").value;
function processText () {
var x = document.getElementById('text').value

if (cache != x) {
socket.emit('data', { text: x, path: pathname });
cache = x;
}
if (cache != x) {
socket.emit('data', { text: x, path: pathname })
cache = x
}
}

+ 7
- 7
routes/index.js View File

@@ -1,11 +1,11 @@
var express = require('express');
var express = require('express')
var shortid = require('shortid')
var router = express.Router();
var router = express.Router()

/* GET home page. */
router.get('/', function(req, res, next) {
var sid = shortid.generate();
res.render('index', { title: 'Welcome to Pad', buttonLbl: 'Get started', id: sid });
});
router.get('/', function (req, res, next) {
var sid = shortid.generate()
res.render('index', { title: 'Welcome to Pad', buttonLbl: 'Get started', id: sid })
})

module.exports = router;
module.exports = router

+ 37
- 46
routes/socket.js View File

@@ -1,85 +1,76 @@
var app = require('express');
var shortid = require('shortid');
var router = app.Router();

module.exports = function(io) {

var welcomeMessage = "Pad created! Share the URL with a friend to edit text in real-time.";
var pages = new Map(); // Stores Pad data
var app = require('express')
var shortid = require('shortid')
var router = app.Router()

module.exports = function (io) {
var welcomeMessage = 'Pad created! Share the URL with a friend to edit text in real-time.'
var pages = new Map() // Stores Pad data

// Handle WebSocket connections
io.on('connection', function(socket) {

console.log("A user connected")
io.on('connection', function (socket) {
console.log('A user connected')

// Handle sync request
socket.on('sync', function (data) {
if (pages.has(data.path)) {

// Page exists, notify with pad data
notify(socket, pages.get(data.path), data.path);
console.log("Paste accessed: " + data.path);

notify(socket, pages.get(data.path), data.path)
console.log('Paste accessed: ' + data.path)
} else {

// Page doesn't exist, notify with new pad
notify(socket, '', data.path);
console.log("Created paste: " + data.path);

notify(socket, '', data.path)
console.log('Created paste: ' + data.path)
}

// Remove junk objects from Map, save space
clearUpMemory();
});
clearUpMemory()
})

// Handle incoming data from user
socket.on('data', function (data) {

// Update pad data in memory
pages.set(data.path, data.text);
var curr = pages.get(data.path);
pages.set(data.path, data.text)
var curr = pages.get(data.path)

// Notify everyone of update
notifyAll(socket, curr, data.path);

});
});
notifyAll(socket, curr, data.path)
})
})

// Send update to client
function notify(socket, c, p) {
socket.emit('notify', { content: c, path: p });
function notify (socket, c, p) {
socket.emit('notify', { content: c, path: p })
}

// Send update to all clients
function notifyAll(socket, c, p) {
socket.broadcast.emit('notify', { content: c, path: p });
function notifyAll (socket, c, p) {
socket.broadcast.emit('notify', { content: c, path: p })
}

// Clean up memory if Map gets too full
function clearUpMemory() {
function clearUpMemory () {
for (var [key, value] of pages) {
if (value == "") {
pages.delete(key);
if (value == '') {
pages.delete(key)
}
}
}

/* GET pad by unique id */
router.get('/:id', function(req, res, next) {
res.render('pad', { title: 'Pad', temp: welcomeMessage });
});
router.get('/:id', function (req, res, next) {
res.render('pad', { title: 'Pad', temp: welcomeMessage })
})

/* Handle POST, redirect to GET pad by unique id */
router.post('/:id', function(req, res, next) {
res.render('pad', { title: 'Pad', temp: welcomeMessage });
});
router.post('/:id', function (req, res, next) {
res.render('pad', { title: 'Pad', temp: welcomeMessage })
})

/* Handle index requests for /Pad */
router.get('/', function(req, res, next) {
var sid = shortid.generate();
res.render('index', { title: 'Welcome to Pad', buttonLbl: 'Get started', id: sid });
});
router.get('/', function (req, res, next) {
var sid = shortid.generate()
res.render('index', { title: 'Welcome to Pad', buttonLbl: 'Get started', id: sid })
})

return router;
return router
}

Loading…
Cancel
Save