Commit | Line | Data |
---|---|---|
1d184b4c BA |
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 sassMiddleware = require('node-sass-middleware'); | |
7 | var favicon = require('serve-favicon'); | |
8 | ||
9 | var router = require('./routes/all'); | |
10 | ||
11 | var app = express(); | |
12 | ||
13 | app.use(favicon(path.join(__dirname, "public", "images", "favicon", "favicon.ico"))); | |
14 | ||
15 | if (app.get('env') === 'development') | |
16 | { | |
17 | // Full logging in development mode | |
18 | app.use(logger('dev')); | |
19 | } | |
20 | else | |
21 | { | |
92342261 BA |
22 | // http://dev.rdybarra.com/2016/06/23/Production-Logging-With-Morgan-In-Express/ |
23 | app.set('trust proxy', true); | |
1d184b4c BA |
24 | // In prod, only log error responses (https://github.com/expressjs/morgan) |
25 | app.use(logger('combined', { | |
26 | skip: function (req, res) { return res.statusCode < 400 } | |
27 | })); | |
28 | } | |
29 | ||
30 | // view engine setup | |
31 | app.set('views', path.join(__dirname, 'views')); | |
32 | app.set('view engine', 'pug'); | |
33 | ||
34 | app.use(express.json()); | |
35 | app.use(express.urlencoded({ extended: false })); | |
36 | app.use(cookieParser()); | |
37 | app.use(sassMiddleware({ | |
38 | src: path.join(__dirname, 'public'), | |
39 | dest: path.join(__dirname, 'public'), | |
40 | indentedSyntax: true, // true = .sass and false = .scss | |
41 | sourceMap: true | |
42 | })); | |
43 | app.use(express.static(path.join(__dirname, 'public'))); | |
44 | ||
45 | app.use('/', router); | |
46 | ||
47 | // catch 404 and forward to error handler | |
48 | app.use(function(req, res, next) { | |
49 | next(createError(404)); | |
50 | }); | |
51 | ||
52 | // error handler | |
53 | app.use(function(err, req, res, next) { | |
54 | // set locals, only providing error in development | |
55 | res.locals.message = err.message; | |
56 | res.locals.error = req.app.get('env') === 'development' ? err : {}; | |
57 | // render the error page | |
58 | res.status(err.status || 500); | |
59 | res.render('error'); | |
60 | }); | |
61 | ||
62 | module.exports = app; |