app.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var express = require('express');
  2. var path = require('path');
  3. var favicon = require('static-favicon');
  4. var logger = require('morgan');
  5. var cookieParser = require('cookie-parser');
  6. var bodyParser = require('body-parser');
  7. var routes = require('./routes/index');
  8. var users = require('./routes/users');
  9. var app = express();
  10. // view engine setup
  11. app.set('views', path.join(__dirname, 'views'));
  12. app.set('view engine', 'jade');
  13. app.use(favicon());
  14. app.use(logger('dev'));
  15. app.use(bodyParser.json());
  16. app.use(bodyParser.urlencoded());
  17. app.use(cookieParser());
  18. app.use(express.static(path.join(__dirname, 'public')));
  19. app.use('/', routes);
  20. app.use('/users', users);
  21. /// catch 404 and forwarding to error handler
  22. app.use(function(req, res, next) {
  23. var err = new Error('Not Found');
  24. err.status = 404;
  25. next(err);
  26. });
  27. /// error handlers
  28. // development error handler
  29. // will print stacktrace
  30. if (app.get('env') === 'development') {
  31. app.use(function(err, req, res, next) {
  32. res.status(err.status || 500);
  33. res.render('error', {
  34. message: err.message,
  35. error: err
  36. });
  37. });
  38. }
  39. // production error handler
  40. // no stacktraces leaked to user
  41. app.use(function(err, req, res, next) {
  42. res.status(err.status || 500);
  43. res.render('error', {
  44. message: err.message,
  45. error: {}
  46. });
  47. });
  48. module.exports = app;