Permalink
Browse files

Restored routes and added sockets to them.

The IO object is passed when the route is required, making for a
clunky, yet structure preserving, solution.
  • Loading branch information...
jibby0 committed Jul 30, 2017
1 parent 34ba93d commit e61e1448da7ac640bc02c164e37ba56a518360be
Showing with 776 additions and 595 deletions.
  1. +71 −0 app.js
  2. +90 −0 bin/www
  3. +0 −243 index.js
  4. +389 −346 package-lock.json
  5. +9 −6 package.json
  6. +190 −0 routes/index.js
  7. +18 −0 routes/player-demo.js
  8. +9 −0 routes/users.js
71 app.js
@@ -0,0 +1,71 @@
// express and http
var express = require('express');
var app = express();
// Create the server here, so socket.io can control it.
var server = require('http').Server(app);

var io = require('socket.io')(server);

// view engine setup
var path = require('path');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

// logging
var logger = require('morgan');
app.use(logger('dev'));

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

// Unused (for now) cookies and favicons
//var favicon = require('serve-favicon');
//var cookieParser = require('cookie-parser');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
//app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// // Add socket.io to res in the event loop.
// app.use(function(req, res, next){
// res.io = io;
// next();
// });

// Add the route.
app.use('/', routes);


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

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});

// Export the server to bin/www, since we're creating it here.
module.exports = {app: app, server: server, io: io};

90 bin/www
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app').app;
var debug = require('debug')('lan-jukebox:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = require('../app').server;

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
Oops, something went wrong.

0 comments on commit e61e144

Please sign in to comment.