app.js 1.5 KB

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