app.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. var createError = require('http-errors');
  2. var express = require('express');
  3. var path = require('path');
  4. var cookieParser = require('cookie-parser');
  5. var logger = require('morgan');
  6. var indexRouter = require('./routes/index');
  7. var usersRouter = require('./routes/users');
  8. var app = express();
  9. // view engine setup
  10. app.set('views', path.join(__dirname, 'views'));
  11. app.set('view engine', 'jade');
  12. app.use(logger('dev'));
  13. app.use(express.json());
  14. app.use(express.urlencoded({ extended: false }));
  15. app.use(cookieParser());
  16. app.use(express.static(path.join(__dirname, 'public')));
  17. app.use('/', indexRouter);
  18. app.use('/users', usersRouter);
  19. // catch 404 and forward to error handler
  20. app.use(function(req, res, next) {
  21. next(createError(404));
  22. });
  23. // error handler
  24. app.use(function(err, req, res, next) {
  25. // set locals, only providing error in development
  26. res.locals.message = err.message;
  27. res.locals.error = req.app.get('env') === 'development' ? err : {};
  28. // render the error page
  29. res.status(err.status || 500);
  30. res.render('error');
  31. });
  32. module.exports = app;