app.js 1.4 KB

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