app.js 1.2 KB

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