app.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var express = require('express');
  2. var path = require('path');
  3. var favicon = require('serve-favicon');
  4. var logger = require('morgan');
  5. var cookieParser = require('cookie-parser');
  6. var bodyParser = require('body-parser');
  7. var index = require('./routes/index');
  8. var users = require('./routes/users');
  9. var cards = require('./routes/cards');
  10. var matchmaking = require('./routes/matchmaking');
  11. const Middlewares = require('./middlewares');
  12. var app = express();
  13. // view engine setup
  14. app.set('views', path.join(__dirname, 'views'));
  15. app.set('view engine', 'pug');
  16. // uncomment after placing your favicon in /public
  17. //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
  18. app.use(logger('dev'));
  19. app.use(bodyParser.json());
  20. app.use(bodyParser.urlencoded({ extended: false }));
  21. app.use(cookieParser());
  22. app.use(express.static(path.join(__dirname, 'public')));
  23. // Setup param parsers
  24. const tokenParser = Middlewares.Authorization.getTokenParser();
  25. app.use(tokenParser);
  26. // SETUP ROUTES
  27. app.use('/cards', cards);
  28. app.use('/users', users);
  29. app.use('/matchmaking', matchmaking);
  30. app.use('/', index);
  31. // catch 404 and forward to error handler
  32. app.use(function(req, res, next) {
  33. var err = new Error('Not Found');
  34. err.status = 404;
  35. next(err);
  36. });
  37. // error handler
  38. app.use(function(err, req, res, next) {
  39. // set locals, only providing error in development
  40. res.locals.message = err.message;
  41. res.locals.error = req.app.get('env') === 'development' ? err : {};
  42. // render the error page
  43. res.status(err.status || 500);
  44. res.render('error');
  45. });
  46. module.exports = app;