app.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // express and http
  2. var express = require('express');
  3. var app = express();
  4. // Create the server here, so socket.io can control it.
  5. var server = require('http').Server(app);
  6. var io = require('socket.io')(server);
  7. // view engine setup
  8. var path = require('path');
  9. app.set('views', path.join(__dirname, 'views'));
  10. app.set('view engine', 'hbs');
  11. // logging
  12. var logger = require('morgan');
  13. app.use(logger('dev'));
  14. var routes = require('./routes/index')(io);
  15. // Unused (for now) cookies and favicons
  16. //var favicon = require('serve-favicon');
  17. //var cookieParser = require('cookie-parser');
  18. // uncomment after placing your favicon in /public
  19. //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
  20. //app.use(cookieParser());
  21. app.use(express.static(path.join(__dirname, 'public')));
  22. // Add the route.
  23. app.use('/', routes);
  24. // catch 404 and forward to error handler
  25. app.use(function(req, res, next) {
  26. var err = new Error('Not Found');
  27. err.status = 404;
  28. next(err);
  29. });
  30. // error handlers
  31. // development error handler
  32. // will print stacktrace
  33. if (app.get('env') === 'development') {
  34. app.use(function(err, req, res, next) {
  35. res.status(err.status || 500);
  36. res.render('error', {
  37. message: err.message,
  38. error: err
  39. });
  40. });
  41. }
  42. // production error handler
  43. // no stacktraces leaked to user
  44. app.use(function(err, req, res, next) {
  45. res.status(err.status || 500);
  46. res.render('error', {
  47. message: err.message,
  48. error: {}
  49. });
  50. });
  51. // Export the server to bin/www, since we're creating it here.
  52. module.exports = {app: app, server: server, io: io};