app.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. var express = require('express'),
  2. path = require('path'),
  3. favicon = require('serve-favicon'),
  4. logger = require('morgan'),
  5. cookieParser = require('cookie-parser'),
  6. bodyParser = require('body-parser'),
  7. config = require('./config'),
  8. i18n = require("i18n");
  9. var routes = require('./routes/index');
  10. var app = express();
  11. i18n.configure({
  12. defaultLocale: "en",
  13. directory: __dirname + '/locales',
  14. autoReload: true
  15. });
  16. i18n.setLocale(config.locale);
  17. // default: using 'accept-language' header to guess language settings
  18. app.use(i18n.init);
  19. // view engine setup
  20. app.set('views', path.join(__dirname, 'views'));
  21. app.set('view engine', 'jade');
  22. // uncomment after placing your favicon in /public
  23. //app.use(favicon(__dirname + '/public/favicon.ico'));
  24. app.use(logger('dev'));
  25. app.use(bodyParser.json());
  26. app.use(bodyParser.urlencoded({ extended: false }));
  27. app.use(cookieParser());
  28. app.use(express.static(path.join(__dirname, 'public')));
  29. app.use('/', routes);
  30. // catch 404 and forward to error handler
  31. app.use(function(req, res, next) {
  32. var err = new Error('Not Found');
  33. err.status = 404;
  34. next(err);
  35. });
  36. // error handlers
  37. // development error handler
  38. // will print stacktrace
  39. if (app.get('env') === 'development') {
  40. app.use(function(err, req, res, next) {
  41. res.status(err.status || 500);
  42. res.render('error', {
  43. message: err.message,
  44. error: err
  45. });
  46. });
  47. }
  48. // production error handler
  49. // no stacktraces leaked to user
  50. app.use(function(err, req, res, next) {
  51. res.status(err.status || 500);
  52. res.render('error', {
  53. message: err.message,
  54. error: {}
  55. });
  56. });
  57. module.exports = app;