2018-08-10 19:25:29 +03:00
|
|
|
const createError = require('http-errors');
|
|
|
|
const express = require('express');
|
|
|
|
const path = require('path');
|
|
|
|
const cookieParser = require('cookie-parser');
|
|
|
|
const logger = require('morgan');
|
|
|
|
|
2018-08-17 13:58:44 +03:00
|
|
|
const routes = require('./routes');
|
2018-08-10 19:25:29 +03:00
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
|
|
|
// view engine setup
|
|
|
|
app.set('views', path.join(__dirname, 'views'));
|
|
|
|
app.set('view engine', 'twig');
|
|
|
|
|
|
|
|
app.use(logger('dev'));
|
|
|
|
app.use(express.json());
|
2018-08-17 13:58:44 +03:00
|
|
|
app.use(express.urlencoded({extended: true}));
|
2018-08-10 19:25:29 +03:00
|
|
|
app.use(cookieParser());
|
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
|
2018-08-17 13:58:44 +03:00
|
|
|
app.use('/', routes);
|
2018-08-10 19:25:29 +03:00
|
|
|
// catch 404 and forward to error handler
|
2018-08-17 13:58:44 +03:00
|
|
|
app.use(function (req, res, next) {
|
2018-08-10 19:25:29 +03:00
|
|
|
next(createError(404));
|
|
|
|
});
|
|
|
|
|
|
|
|
// error handler
|
2018-08-17 13:58:44 +03:00
|
|
|
app.use(function (err, req, res, next) {
|
2018-08-10 19:25:29 +03:00
|
|
|
// set locals, only providing error in development
|
|
|
|
res.locals.message = err.message;
|
|
|
|
res.locals.error = req.app.get('env') === 'development' ? err : {};
|
|
|
|
|
|
|
|
// render the error page
|
|
|
|
res.status(err.status || 500);
|
|
|
|
res.render('error');
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = app;
|