app.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 cons = require('consolidate')
  7. var app = express();
  8. // view engine setup
  9. app.set('examples', path.join(__dirname, 'examples'));
  10. app.engine('html', cons.handlebars);
  11. app.set('view engine', 'html');
  12. app.use(logger('dev'));
  13. app.use(express.json());
  14. app.use(express.urlencoded({ extended: false }));
  15. app.use(cookieParser());
  16. app.use('/src', express.static(path.join(__dirname, '/src')));
  17. app.use('/lib', express.static(path.join(__dirname, '/lib')));
  18. app.use('/build', express.static(path.join(__dirname, '/build')));
  19. app.use('/img', express.static(path.join(__dirname, '/img')));
  20. app.use('/origin', express.static(path.join(__dirname, '/origin')));
  21. app.use(express.static(path.join(__dirname, 'examples')));
  22. // catch 404 and forward to error handler
  23. app.use(function (req, res, next) {
  24. next(createError(404));
  25. });
  26. // error handler
  27. app.use(function (err, req, res, next) {
  28. // set locals, only providing error in development
  29. res.locals.message = err.message;
  30. res.locals.error = req.app.get('env') === 'development' ? err : {};
  31. // render the error page
  32. res.status(err.status || 500);
  33. res.render('error');
  34. });
  35. module.exports = app;