app.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import express from 'express';
  2. import path from 'path';
  3. import bodyParser from 'body-parser';
  4. import favicon from 'serve-favicon';
  5. import logger from 'morgan';
  6. import winston from './winston';
  7. import routes from './routes/routes';
  8. const rootDir = process.env.NODE_ENV === 'development' ? 'build' : 'dist';
  9. const staticPath = path.join(__dirname, rootDir);
  10. const app = express();
  11. app.set('views', path.join(__dirname, 'views'));
  12. app.set('view engine', 'jade');
  13. app.use(favicon(path.join(__dirname, rootDir + '/img/favicon.ico')));
  14. app.use(logger('dev'));
  15. app.use(bodyParser.json());
  16. app.use(bodyParser.urlencoded({extended: true}));
  17. app.use(express.static(staticPath));
  18. app.use('/', routes);
  19. app.use((req, res, next) => {
  20. let err = new Error('Not Found');
  21. err.status = 404;
  22. next(err);
  23. });
  24. app.use((err, req, res, next) => {
  25. res.status(err.status || 500);
  26. winston.log('error', err.message);
  27. res.render('error', {
  28. message: err.message,
  29. error: app.get('env') === 'development' ? err : {}
  30. });
  31. });
  32. export default app;